gpt4 book ai didi

java - 进度通知未取消

转载 作者:行者123 更新时间:2023-12-02 13:07:18 26 4
gpt4 key购买 nike

当我按通知中的“取消”按钮时,通知仍保留在通知栏中,每次按“取消”时,通知都会关闭,但仍显示在顶部栏中,当我打开通知时仍然存在,通知没有被正确删除。

这是创建进度通知的代码,它工作正常:

private void createProgressNotification() {

PendingIntent dismissIntent = NotificationActivity.getDismissIntent(NOTIFICATION_PROGRESS, Analyse.this);

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setContentTitle("URL Analysis")
.setContentText("Analysis in progress")
.setSmallIcon(R.drawable.icon_notification)
.setAutoCancel(false)
.addAction(R.drawable.cancel, "Cancel", dismissIntent)
.setOngoing(true)
.setProgress(0, 0, true);

NotificationManager mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.notify(NOTIFICATION_PROGRESS, mBuilder.build());
}

这是我必须处理的自定义NotificationActivity类:

public class NotificationActivity extends Activity {

public static final String NOTIFICATION_ID = "NOTIFICATION_ID";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(getIntent().getIntExtra(NOTIFICATION_ID, -1));
finish(); // since finish() is called in onCreate(), onDestroy() will be called immediately
}

public static PendingIntent getDismissIntent(int notificationId, Context context, AsyncCallWS analyze) {
Intent intent = new Intent(context, NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(NOTIFICATION_ID, notificationId);
PendingIntent dismissIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

return dismissIntent;
}

}

知道为什么会发生这种情况吗?

最佳答案

在NotificationCompat Builder属性的setAutoCancel()中将false更改为true

  mBuilder.setAutoCancel(true);

原来如此

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setContentTitle("URL Analysis")
.setContentText("Analysis in progress")
.setSmallIcon(R.drawable.icon_notification)
.setAutoCancel(true)
.addAction(R.drawable.cancel, "Cancel", dismissIntent)
.setOngoing(true)
.setProgress(0, 0, true);

关于java - 进度通知未取消,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44095941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com