通知会在每次进度更新时振动

时间:2017-10-03 21:46:05

标签: android notifications

在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());
  }

2 个答案:

答案 0 :(得分:28)

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

答案 1 :(得分:0)

尝试禁用这样的振动:

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