我正在制作的应用程序具有文件传输功能,我在通知中显示其进度,并带有停止传输的选项。问题是每次调用NotificationManager.notify
(并且每次进度更改时都必须调用它),播放系统通知声音(这会导致大量垃圾邮件),并且停止传输的按钮变得不可用。
不更新进度条允许按下"停止传输"按钮。我注意到的另一件事是,Android 8.x上存在垃圾声音,但在5.x上不存在。
创建通知:
mBuilder = new NotificationCompat.Builder(cont, "default")
.setSmallIcon(R.drawable.file)
.setContentTitle("File transfer app")
.setContentText((down ? "Downloading " : "Uploading ") + fileName)
.setAutoCancel(false)
.setProgress(100, 0, indeterminate);
Intent notifyButton = new Intent();
notifyButton.setAction("STOP_TRANSFER");
notifyButton.putExtra("id", notifId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(cont, notifId, notifyButton, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.addAction(R.drawable.close, "Stop transfer", pendingIntent);
all.add(this);
mNotificationManager.notify(notifId, mBuilder.build());
更新其进度:
mBuilder.setProgress(this.max, progress, indeterminate);
mNotificationManager.notify(notifId, mBuilder.build());
我希望进度条能够正常更新,每次移动时都不会发出声音,并允许我随时取消传输。
我在这里做错了什么吗?
如果你的更新通知不断,你可能需要调用mNotificationManager.cancel()来通知进度。
if(update){
mNotificationManager.cancel(notifId)
}
mNotificationManager.notify(notifId, mBuilder.build());