问题描述
我已经阅读了许多有关如何创建通知消息的示例.我想要实现的是因为通知将由小部件执行,我想当用户点击它时,点击时的通知意图会自行清除.我没有要返回的活动.出于我的目的的通知只会简单地通知,没有别的.那么,只是清除/取消自身的意图的代码是什么.下面的代码是一个按钮启动的活动(不包括按钮代码),通知将由后台服务启动.
I have read many examples of how to create notification messages.What i wanted to achieve, is because the notification will be executed by a widget, i would likethe notification intent when clicked to clear it self when the user clicks on it.I do not have an activity to return to.The notification for my purposes will just plainly notify, nothing else.So what would be the code of an intent that just clear/cancel itself.The code below is an activity launched by a button(button code not included) the notification will be fired up by a background service.
CharSequence title = "Hello";
CharSequence message = "Hello, Android!";
final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notification = new Notification(R.drawable.icon,"A New Message!",System.currentTimeMillis());
notification.defaults=Notification.FLAG_ONLY_ALERT_ONCE+Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(this, AndroidNotifications.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,notificationIntent, 0);
notification.setLatestEventInfo(AndroidNotifications.this, title,message, pendingIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
谢谢
推荐答案
如果在用户单击通知时应取消通知,则将按位或运算到标志字段中的位应设置该位.
Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user.
notification.flags |= Notification.FLAG_AUTO_CANCEL;
这篇关于Android通知意图自行清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!