如何正确地清除所有通知一旦点击? [英] How to properly clear all notification once clicked?
本文介绍了如何正确地清除所有通知一旦点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发送通知栏上的几个通知,我想清楚这一切时,点击通知之一。现在我用标志清除一个接一个。我知道 notificationManager.cancelAll()
可以清除所有通知,但我应该在哪里放,这样我可以触发一次被点击通知中的一个。
私有静态无效generateNotification(上下文的背景下,字符串消息){
INT图标= R.drawable.ic_launcher;
时长= System.currentTimeMillis的();
NotificationManager notificationManager =(NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
通知通知=新的通知(图标,消息,时);
字符串title = context.getString(R.string.app_name);
意图notificationIntent =新意图(背景下,MainActivity.class); //设置的意图,因此它不会启动新活动
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
的PendingIntent意图=
PendingIntent.getActivity(上下文,0,notificationIntent,0);
notification.setLatestEventInfo(上下文,标题,邮件,意图);
notification.flags | = Notification.FLAG_AUTO_CANCEL; notificationManager.notify(MSGID,通知);
//notificationManager.cancelAll(); // i湾被点击的通知时,清除所有,在那里我应该把这一行?
}
解决方案
我的解决办法是在 onResume称之为()
。
@覆盖
保护无效onResume(){
super.onResume();//清除所有通知
NotificationManager nMgr =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}
I send a few notification on the notification bar, i wanted to clear all of it when one of the notification is clicked. For now I clear one by one by using Flag. I know notificationManager.cancelAll()
could clear all the notification but where should i put so that i can trigger once one of the notification is clicked.
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(msgid, notification);
//notificationManager.cancelAll(); //i wan to clear all when the notification is clicked, where should i put this line?
}
解决方案
My solution is to call it at onResume()
.
@Override
protected void onResume() {
super.onResume();
// Clear all notification
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
}
这篇关于如何正确地清除所有通知一旦点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文