【问题标题】:Notification vibrates on each progress update每次进度更新时都会振动通知
【发布时间】:2018-03-15 04:50:56
【问题描述】:

在 Android 8.0 (Oreo) 上,每次通知进度有更新时,它都会振动。所以手机在这个过程中振动了100次。这只发生在 Android 8.0 上所以我可以假设我对他们的 API 有一些不当使用。我正在寻求帮助以停止通知的每次进度更新时的振动。这是我的代码:

建筑通知

mNotifyManager = (NotificationManager) mActivity.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) createChannel(mNotifyManager);
mBuilder = new NotificationCompat.Builder(mActivity, "FileDownload")
        .setSmallIcon(android.R.drawable.stat_sys_download)
        .setColor(ContextCompat.getColor(mActivity, R.color.colorNotification))
        .setContentTitle(mFile.getName())
        .setContentText(mActivity.getResources().getString(R.string.app_name))
        .setProgress(0, 0, true);
mNotifyManager.notify(mFile.getId().hashCode(), mBuilder.build());

创建通道方法

  @TargetApi(26)
  private void createChannel(NotificationManager notificationManager) {
    String name = "FileDownload";
    String description = "Notifications for download status";
    int importance = NotificationManager.IMPORTANCE_MAX;

    NotificationChannel mChannel = new NotificationChannel(name, name, importance);
    mChannel.setDescription(description);
    mChannel.enableLights(true);
    mChannel.setLightColor(Color.BLUE);
    notificationManager.createNotificationChannel(mChannel);
  }

onProgress

  @Override
  public void onProgress(File file, double progress, long downloadedBytes, long totalBytes) {
    mBuilder.setProgress(100, (int) (progress * 100), false);
    mNotifyManager.notify(file.getId().hashCode(), mBuilder.build());
  }

【问题讨论】:

标签: android notifications


【解决方案1】:

在最初构建通知时在 NotificationCompat.Builder 上使用 setOnlyAlertOnce(true) 应该可以解决问题。

【讨论】:

    【解决方案2】:

    尝试像这样禁用振动:

    notificationBuilder.setDefaults(Notification.DEFAULT_LIGHT | Notification.DEFAULT_SOUND)
                       .setVibrate(new long[]{0L}); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 2018-05-07
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      相关资源
      最近更新 更多