当前位置:操作系统 > 安卓/Android >>

Notification及NotificationManager的使用详解

通知是应用程序通知用户的一种方式,它无须活动,由通知管理器进行统一管理。通知包含一下功能:
1.      创建新的状态栏图标
2.      在扩展的状态栏窗口显示额外的信息(可以发起一个意图)
3.      闪烁/LED
4.      让手机震动
5.      发出声音(铃声,媒体库歌曲)
通知管理器是用来处理通知的系统服务,使用getSystemService方法可以获得对它的引用,如下:
 
NotificationManager notificationManager = (NotificationManager) context.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
 
通过使用通知管理器,可以触发新的通知,修改现有的通知或者删除那些不再需要的通知。首先创建一个新的Notification对象并传递给它要在状态栏显示的图标、状态栏的点击文本以及这个通知的时间。可以设置Notification对象的number属性来显示一个状态栏图标所表示的事件的数量。
Notification notification = new Notification(R.drawable.icon,"在EoeAndroidReceiver1中", System.currentTimeMillis());
可以通过两张方式在扩展的状态窗口配置通知的外观。
1.      使用setLatestEventInfo方法更新标准的扩展的状态通知显示中所显示的详细信息。
2.      使用一个远程视图(Remote View)设置contentView和contentIntent,以便为扩展的状态显示分配一个定制的UI。
最简单的方法是使用setLatestEventInfo方法来填充默认的状态窗口布局。标准的扩展的状态窗口布局会显示构造函数中定义的图标和时间,以及标题和一个详细信息字符串。
 
notification.setLatestEventInfo(context, "在EoeAndroidReceiver1中", null,contentIntent);
 
通知常用于请求用户的动作或注意,所以可以指定一个PendingIntent,当用户单击通知项的时候触发它,在大多数情况下,该意图应该打开应用程序,并导航到为通知提供了上下文的活动。
 
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,new Intent(context, ActivityMain.class), 0);
notification.setLatestEventInfo(context, "在EoeAndroidReceiver1中", null,contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
 
 
向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合:
Notification.DEFAULT_LIGHTS
Notification.DEFAULT_SOUND
Notification.DEFAULT_VIBRATE
 
如果想全部使用默认值,可以使用Notification.DEFAULT_ALL常量。
Notification.defaults =Notificaiton.DEFAULT_ALL;
 
通过向sound属性分配一个位置URI,android可以将手机上的任意音频文件作为通知进行播放。要使用自己定制的音频,需要把它复制到设备上,或者把它包含在原始资源(raw)中。
Uri uri =RingtoneManager.getDefaultUri(RingtongManager.TYPE_NOTIFICATION);
Notification.sound=uri;
 
可以使用电话的振动功能,让通知执行指定类型的振动。Android可以控制振动的类型,可以使用振动来传递信息或者吸引用户的注意。
要设置振动类型,可以向通知的vibrate属性分配一个longs类型的数组:构造该数组,可以使得代表振动时间的值和代表暂停时间的值交替存在。使用振动必须添加权限:
<uses-permissionandroid:name=”android.permission.VIBRATE”/>
Long[] vibrate = newlong[]{1000,1000,1000,1000,1000};
notification.vibrate=vibrate;
 
通知还可以包含用来配置设备的LED的颜色和闪烁频率的属性。
每个设备在对LED的控制方面可能具有不同的限制。如果指定的颜色可用,则将使用一个与指定颜色最接近的颜色。
ledARGB属性可以用来设置LED颜色,而ledOffMS和ledOnMS属性则可以设置LED闪烁的频率和模式。可以通过把ledOnMS属性设置为1,并且把ledOffMS设置为0来打开LED,或者也可以通过把这两个属性都设置为0来关闭LED。
一旦配置了LED设置,就必须在通知的flags属性中添加FLAG_SHOW_LIGHTS标记。
notification.ledARGB = Color.RED
notification.ledOffMS = 0;
notification.ledOnMS = 1;
notification.flags = notification.flags |Notification.FLAG_SHOW_LIGHTS;
 
通过设置通知的FLAG_INSISTENT和FLAG_ONGOING_EVENT标记,可以把它配置为持续的或者连续的。
持续的通知会一直重复音频、振动和闪灯设置,只到被取消为止。
 
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;
     或者可以自己的LED提醒模式:
    notification.ledARGB = 0xff00ff00;
    notification.ledOnMS = 300; //亮的时间
    notification.ledOffMS = 1000; //灭的时间
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
 
 
* 更多的特征属性
* notification.flags |= FLAG_AUTO_CANCEL; //在通知栏上点击此通知后自动清除此通知
* notification.flags |= FLAG_INSISTENT; //重复发出声音,直到用户响应此通知
* notification.flags |= FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中
* notification.flags |= FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,
* //经常与FLAG_ONGOING_EVENT一起使用
* notification.number = 1;
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,