![](/img/trans.png)
[英]Cancelled Notification being redelivered when other notification is touched. Android
[英]Progress Notification not being cancelled
當我按通知中的“取消”按鈕時,通知仍保留在通知欄中,每當我按“取消”時,通知將關閉但仍顯示在頂部欄中,而當我打開通知時,通知未正確刪除。
這是創建進度通知的代碼,它可以正常工作:
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());
}
這是我必須處理dismissIntent的自定義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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.