隐藏正在进行的通知

时间:2011-11-09 11:50:12

标签: java android

我有以下代码来创建ONGOING_EVENT

Notification notification = new Notification(R.drawable.icon, "someText" , System.currentTimeMillis());
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);
notification.setLatestEventInfo(this, "someTitle", "someText", contentIntent);

notificationManager.notify(SOME_ID, notification);

现在,我想在以后杀死它:

notificationManager.cancel(SOME_ID);

但是,通知仍然存在。可能是什么原因造成的?

2 个答案:

答案 0 :(得分:3)

问题是notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;。我误解了此标志的文档,随后无法删除通知。该标志通常由以startForeground(int id, Notification notification)开头的服务设置,并阻止在stopForeground(boolean removeNotification)停止服务之前删除通知。

我现在使用startForeground / stopForeground方法,显示/隐藏通知完全没问题。

答案 1 :(得分:0)

我还没有实现此标记,但请检查通知标记FLAG_AUTO_CANCEL。在创建通知之前添加此项。

相关问题