通知:
1:一般用在广播或者service中,用于简要消息的推送;
2:有三个基本特征:1:在状态栏有提示图标;2:下拉列表中看到基本的简要信息;3:在点击之后能够跳转到详情页;
创建:
//通知是通过NotificationManger来管理的:
NotificationManger noteManger = (NotificationManger)getSystemService(Context.NOTIFICATION_SERVICE);
对通知对象的第一种创建方式:
//Notification对象用于存储该通知的一些基本信息;
Notification notify1 = new Notification(drable.r.icon,”title”,System.currentTimeMillis());
notify1.setLatesEventInfo(context,”title”,”content”,PaddingIntent paddingintent);
noteManger.notify(int id,notify1);
对通知对象的第二种创建方式:
Notification notify2 = new Notification();
notify2.icon;
notify2.tickerText;
notify2.when;
notification.setLatesEventInfo(context,”title”,”content”,PaddingIntent paddingintent);
notify2.flags |= Notification.FLAG_CANCEL_CURRENT;
noteManger.notify(id,notify2);
对通知象创建的第三种方式:API11以上
Notification notify = new Notification.Builder(this)
.setSmallIcon(R.drawable.notify)//设置状态栏的小图标
.setTicker(“statu—msg”)//设置状态栏的提示
.setContentTitle(“notification title”)//设置下拉之后的提示
.setContetnText(“content values”)//设置下拉之后的正文内容
.setContetnIntent(pendingIntent2) //关联intent
.setNumber(1)//在通知栏显示数字,是个序号,如果触发显示同一个id则会通过这个选择显示哪一个
.getNotification();
notify3.flags |= Notification.FLAG_CANCEL_CURRENT;
noteManger.notify(id,notify3);
对通知象创建的第四种方式:API16以上:
Notification notify3 = new Notification.Builder(this)
.setSmallIcon(R.drawable.message) //设置状态栏的小图标
.setTicker(“TickerText:” + “您有新短消息,请注意查收!”) //设置状态栏的提示
.setContentTitle(“Notification Title”) //设置下拉之后的提示
.setContentText(“This is the notification message”) //设置下拉之后的正文内容
.setContentIntent(pendingIntent3) //关联intent
.setNumber(1)//在通知栏显示数字,是个序号,如果触发显示同一个id则会通过这个选择显示哪一个
.build(); //此处换成了build来创建notify;而不是getNotification();
notify3.flags |= Notification.FLAG_CANCEL_CURRENT;
noteManger.notify(id,notify3);
对通知象创建的第四种方式:
Notification notify4 = new Notification();
//设置通知的基本属性
notify4.icon = R.drawable.message;
notify4.tickerText = “TickerText:您有新短消息,请注意查收!”;
notify4.when = System.currentTimeMillis();
notify4.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
//自定义通知的view样式和内容
notify4.contentView = rv; //rv是一个布局转换成的view;
Intent intent = new Intent(Intent.ACTION_MAIN);
PendingIntent contentintent = PendingIntent.getActivity(this, 1,intent, 1);
notify4.contentIntent = contentintent;
manager.notify(NOTIFICATION_FLAG, notify4);
PendingIntent:
区别:
和Intent相同,都可以发送广播,启动activity,service;
但是Intent趋于立即执行该动作,PendingIntent则趋向于达到某个条件之后执行;
使用:
创建实例:
PendingIntent pintent = PendingIntent.getActivity();
PendingIntent pintent = PendingIntent.getBroadcast();
PendingIntent pintent = PendingIntent.getService();
参数:
第一个参数是Context;
第二个参数是:
第三个参数是intent对象:Intetn intent = new Intent(FirstActivity.this,NoteActivity.class);
第四个参数是该intent对象的行为:
FLAG_ONE_SHOT:
FLAG_NO_CREATE:
FLAG_CANCEL_CURRENT:当通知被点击之后会清除
FLAG_UPDATE_CURRENT
如:
PendingIntent pintent = PendingIntent.getActivity(this,0,intent,FLAG_CANCEL_CURRENT);
传递参数:
notification.setLatesEventInfo(this,”title”,”content”,pintent);
取消通知:
noteManger.cancel(id);//清除id为x的通知
manager.cancelAll();//清除所有通知