我阅读了许多关于如何创建通知消息的示例。 我的目标是当小部件触发通知时,希望用户在单击通知意图后可以清除它自己。因为我没有要返回的活动,所以此处的通知只是纯粹的提醒,不需要做其他事情。 那么,有没有一种只能清除/取消自身的意图代码呢? 下面的代码是由按钮启动的活动(按钮代码未包含)。通知将由后台服务触发。
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);
感谢
notification.flags |= Notification.FLAG_AUTO_CANCEL;
- Pentium10