我已经阅读了许多如何创建通知消息的示例。 我想达到的目的是因为通知会由一个小部件执行,我想在点击时通知用户的意图是当用户点击它时自行清除它。我没有要返回的活动。 我的目的通知只是明确地通知,没有别的。 那么,一个意图的代码是什么,只是清除/取消自己。 下面的代码是一个由按钮启动的活动(不包括按钮代码),通知将由后台服务启动。Android通知意图清除它自己
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_ID”。 – Pentium10 2010-08-13 18:17:21
是的,我会从一个按钮调用它,但是我想在用户选择通知时从代码中调用它。 – John 2010-08-14 09:18:06
工作为我解决方案,但有点混乱(额外的服务要写)。但是在服务中,我可以访问调用通知ID作为参数的Intent。 – 2012-02-20 15:17:56