Android笔记:通知

可以在活动里创建,也可以在广播接收器里创建,还可以在服务里创建。

NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

创建一个Notification 对象就可以写成:
Notification notification = new Notification(R.drawable.icon, "——————This is ticker text——————",System.currentTimeMillis());

                      图标                    刚出的时候一闪而过             创建的时间

notification.setLatestEventInfo(context, "This is content title", "This iscontent text", null);

                     标题         正文

manager.notify(1, notification);//数字表示ID,每个消息的ID应不同。

 

PendingIntent主要提供了几个静态方法用于获取PendingIntent的实例,可以根据需求来选择是使用getActivity()方法、getBroadcast()方法、还是getService()方法。

第一个参数是Context。第二个参数传入0 即可。第三个参数是一个Intent 对象,可以通过这个对象构建出PendingIntent 的“意图”。

第四个参数用于确定PendingIntent 的行为,有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT 和FLAG_UPDATE_CURRENT 这四种值可选。

Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, "This is content title","This is content text", pi);//第四个参数是调用pendingintent的。

 

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);//取消通知 “1"就是一开始传入的ID。

posted @ 2015-08-20 16:43  临轩望月  阅读(161)  评论(0编辑  收藏  举报