Android的通知意向将其清除自身将其、意向、通知、Android

2023-09-13 00:05:11 作者:放逐

我已经阅读了如何创建通知消息的例子很多。 我想要的东西来实现,是因为该通知将通过一个小部件来执行,我想 通知意图时点击时,用户点击它。我不具有活性,返回到以清除它自己。 通知我的目的只是清楚地告知,没有别的。 那么,什么将是刚刚疏通/取消自身意图的code。 在code以下是通过一个按钮启动(不包括按钮code)该通知将通过一个后台服务被解雇了。一个活动

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 like the 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);

感谢

推荐答案

查看FLAG_AUTO_CANCEL

位进行按位或为它被点击时,用户应设置在通知应取消标志字段。

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;