代码之家  ›  专栏  ›  技术社区  ›  Adrian Le Roy Devezin

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

  •  22
  • Adrian Le Roy Devezin  · 技术社区  · 8 年前

    在安卓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 回复  |  直到 8 年前
        1
  •  56
  •   Henning Dodenhof    8 年前

    setOnlyAlertOnce(true) 在您的 NotificationCompat.Builder 在最初构建通知时,应该做到这一点。

        2
  •  0
  •   james    8 年前

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