码迷,mamicode.com
首页 > 其他好文 > 详细

Notification总结

时间:2015-07-19 23:33:10      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:

如何创建notification
  1>实例化一个NotificationCompat.Builder对象;如builder
  2>调用builder的相关方法对notification进行上面提到的各种设置
  3>调用builder.build()方法此方法返回一个notification对象。
  4>实例化一个NotificationManager对象;如:manager
  5>调用manager的notify方法。

注:
 一个notification不必对上面所有的选项都进行设置,但有3项是必须的:
  小图标, set by setSmallIcon()
  内容标题, set by setContentTitle()
  内容, set by setContentText()

 

如果要添加一个Notification,可以按照以下几个步骤

1:定义一个Notification:
Notification m_Notification=new Notification();

2:设置Notification的各种属性:
//设置通知在状态栏显示的图标
m_Notification.icon=R.drawable.icon;

//当我们点击通知时显示的内容
m_Notification.tickerText="Button1 通知内容.....";

通知时发出的默认声音
m_Notification.defaults=Notification.DEFAULT_SOUND;

//设置通知显示的参数
Intent m_Intent=new Intent(NotificationDemo.this,DesActivity.class);
PendingIntent m_PendingIntent=PendingIntent.getActivity(NotificationDemo.this, 0, m_Intent, 0);
m_Notification.setLatestEventInfo(NotificationDemo.this, "Button1", "Button1通知",m_PendingIntent );

3:获取NotificationManager:
NotificationManager m_NotificationManager=(NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);

4:开始执行这个通知
m_NotificationManager.notify(0,m_Notification);

6:既然可以增加同样我们也可以删除。当然是只是删除你自己增加的。
m_NotificationManager.cancel(0);
这里的0是一个ID号码,和notify第一个参数0一样。这也就完成了,添加删除工作。

 

PendingIntent的使用 Flags的类型:
FLAG_ONE_SHOT:得到的pi只能使用一次,第二次使用该pi时报错
FLAG_NO_CREATE:当pi不存在时,不创建,返回null
FLAG_CANCEL_CURRENT: 每次都创建一个新的pi
FLAG_UPDATE_CURRENT: 不存在时就创建,创建好了以后就一直用它,每次使用时都会更新pi的数据(使用较多)


Notification一些有用的flag与字段:
1.FLAG_AUTO_CANCEL 当用户点击通知栏时通知栏取消。
2.FLAG_INSISTENT 设置的声音会重复响,除非用户清除掉这个通知或者点击进入这个通知所指向的程序
3.FLAG_NO_CLEAR 设置了这个,通知将永不会被取消,就算clear也没用
4.FLAG_ONLY_ALERT_ONCE 设置这个能让通知每发一次就响一次设置好的声音
5.FLAG_SHOW_LIGHTS 设置了这个才能让LED亮(通知不一定只能通知消息,也能通知背光灯闪烁,播放音乐等)
1.audioStreamType 用来播放的音频类型,int类型
2.contentIntent 当点击通知栏时执行的PendingIntent;
3.contentView RemoteViews类型,通知栏的界面
4.flags 标志位,值就设置上面的标志,不设置就是默认的设置。
5.icon 图标,状态栏里的图标。(必须有)
6.number 这个通知显示的次数
7.sound Uri类型值,要播放的声音
8.tickerText 显示在状态栏的文字(不必须)
9.when 时间戳,不设置就是now,用于显示在通知栏

 

------------------------------------------------------------------------------------------------------

NoticificationManager很容易可以放在状态栏,也很容易实现从statusbar进入程序 中,
NoticificationManager中通过intent执行此程序的activity就可以了
NotificationManager(通知管理器):
NotificationManager有三个公共方法:
1. cancel(int id) 取消以前显示的一个通知.假如是一个短暂的通知,试图将隐藏,假如是一个持久的通知,将从状态条中移走.
2. cancelAll() 取消以前显示的所有通知.
3. notify(int id, Notification notification) 把通知持久的发送到状态条上.

//初始化NotificationManager:
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

Notification代表着一个通知.
Notification的属性:
audioStreamType 当声音响起时,所用的音频流的类型
contentIntent 当通知条目被点击,就执行这个被设置的Intent.
contentView 当通知被显示在状态条上的时候,同时这个被设置的视图被显示.
defaults 指定哪个值要被设置成默认的.
deleteIntent 当用户点击"Clear All Notifications"按钮区删除所有的通知的时候,这个被设置的Intent被执行.
icon 状态条所用的图片.
iconLevel 假如状态条的图片有几个级别,就设置这里.
ledARGB LED灯的颜色.
ledOffMS LED关闭时的闪光时间(以毫秒计算)
ledOnMS LED开始时的闪光时间(以毫秒计算)
number 这个通知代表事件的号码
sound 通知的声音
tickerText 通知被显示在状态条时,所显示的信息
vibrate 振动模式.
when 通知的时间戳.

将Notification发送到状态条上:
Notification notification = new Notification();
Notification的设置过程……..
nm.notify(0, notification); //发送到状态条上

------------------------------------------------------------------------------------------------------------

Notification提供了丰富的手机提示方式:

a)在状态栏(Status Bar)显示的通知文本提示,如:
notification.tickerText = "hello";

b)发出提示音,如:
notification.defaults = Notification.DEFAULT_SOUND;
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");

c)手机振动,如:
notification.defaults = Notification.DEFAULT_VIBRATE;
long[] vibrate = {0,100,200,300};
notification.vibrate = vibrate;

d)LED灯闪烁,如:
notification.defaults = Notification.DEFAULT_LIGHTS;
notification.ledARGB = 0xff00ff00;
notification.ledOnMS = 300;
notification.ledOffMS = 1000;
notification.flags = Notification.FLAG_SHOW_LIGHTS;

4)发送通知:
private static final int ID_NOTIFICATION = 1;
mNotificationManager.notify(ID_NOTIFICATION, notification);


也可以使用稍微复杂一些的方式创建Notification:
int icon = R.drawable.notification_icon; //通知图标
CharSequence tickerText = "Hello"; //状态栏(Status Bar)显示的通知文本提示
long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
Notification notification = new Notification(icon, tickerText, when);

通知的更新
如果需要更新一个通知,只需要在设置好notification之后,再调用setLatestEventInfo,然后重新发送一次通知即可。

 

Notification总结

标签:

原文地址:http://www.cnblogs.com/jlyf/p/4659315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!