Android-Notification的使用

来源:互联网 发布:软件系统开发公司 编辑:程序博客网 时间:2024/05/01 04:47

 http://blog.sina.com.cn/s/blog_4a9f789a0100md8v.html

http://blog.csdn.net/haibinglong/archive/2010/09/08/5870255.aspx

 

 

1. NotificationManager和Notification用来设置通知。

通知的设置等操作相对比较简单,基本的使用方式就是用新建一个Notification对象,然后设置好通知的各项参数,然后使用系统后台运行的NotificationManager服务将通知发出来。

基本步骤如下:

1)得到NotificationManager:

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

 

2)创建一个新的Notification对象:

Notification notification = new Notification();

notification.icon = R.drawable.notification_icon;

 

也可以使用稍微复杂一些的方式创建Notification:

int icon = R.drawable.notification_icon; //通知图标

CharSequence tickerText = "Hello";  //状态栏(Status Bar)显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

Notification notification = new Notification(icon, tickerText, when);

 

3)填充Notification的各个属性:

Context context = getApplicationContext();

CharSequence contentTitle = "My notification";

CharSequence contentText = "Hello World!";

Intent notificationIntent = new Intent(this, MyClass.class);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

 

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);

 

2. 通知的更新

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

 

3. 自定义通知视图

   这部分可以参考官方文档,讲的很详细了。

AndroidSDK: docs/guide/topics/ui/notifiers/notifications.html

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/haibinglong/archive/2010/09/08/5870255.aspx

 


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 通知的时间戳.

 

原创粉丝点击