notification使用后的效果,可以先想一下你的手机有个来电未接,然后状态栏上会有个提示,你拉下菜单点击它则可以查看来电信息,notification一般配合notificationmanager使用,这里写了个产生notification的方法:
public void notificationOccur()
{
//first step
NotificationManager nm = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
//second step create Notification and set related state
Notification noti = new Notification(R.drawable.icon,"hello",System.currentTimeMillis());
Intent intent = new Intent(this,NotificationTestActivity.class);
PendingIntent pi = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
noti.setLatestEventInfo(this, "contentTitle", "contentText", pi);
//third step do notification by notification manager
nm.notify(1, noti);
}
基本过程就是分三步,先通过系统服务获取notificationmanager的对象
然后创建notification,然后对notification设置其intent事件,并由PendingIntent来包装
最后通过notificationmanager显示。
不过本例在运行的时候,点击notification时intent事件貌似没有打开NotificationTestActivity,具体原因还需查证,之后再来做修改
发现原因了:在manifest中没有加入对notificationTestActivity的说明,需要加入一个Activity标签