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

Adr*_*zin 16 notifications android

在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());
Run Code Online (Sandbox Code Playgroud)

创建频道方法

  @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);
  }
Run Code Online (Sandbox Code Playgroud)

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());
  }
Run Code Online (Sandbox Code Playgroud)

小智 36

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