Notification的使用方法,其实属性设置
0.获取NotificationManagerNotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);在Android源码Context.java文件中有public static final String NOTIFICATION_SERVICE = "notification";
1.设置标题:String titleStr = context.getString(R.string.norification_control_state_title);
2.设置摘要:String summaryStr = context.getString(R.string.norification_control_state_summary);
Notification标志ID.可以通过这个ID来取消这个通知notificationManager.cancel(notificationID);private static final int notificationID = 1;
3.实例化Notification:Notification notification = new Notification();
4.设置通知的图标:notification.icon = R.drawable.ic_setting_security;
5.设置时间,值为0 时则不显示,也可以设置为系统当前时间System.currentTimeMillis();notification.when = 0;
6.设置flags,这单词自己理解意义吧!翻译成标志似乎并不恰当.FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉FLAG_NO_CLEAR 该通知能不被状态栏的清除按钮给清除掉FLAG_ONGOING_EVENT 通知放置在正在运行FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应源代码Notification.java中public static final int FLAG_AUTO_CANCEL = 0X00000010;其他的就不一一列举了.notification.flags = Notification.FLAG_AUTO_CANCEL;
7.设置状态栏的标题notification.tickerText = titleStr;
8.设置灯光闪烁提醒,.使用默认闪烁notification.defaults |= Notification.DEFAULT_LIGHTS;.使用自定义闪烁notification.ledARGB = 0xff00ff00; // LED灯的颜色,绿灯notification.ledOnMS = 300; // LED灯显示的毫秒数,300毫秒notification.ledOffMS = 1000; // LED灯关闭的毫秒数,1000毫秒notification.flags |= Notification.FLAG_SHOW_LIGHTS; // 必须加上这个标志
notification.defaults = 0
9.设置提示声音,·使用默认声音notification.defaults |= Notification.DEFAULT_SOUND;·使用自定义声音notification.sound = Uri.parse("file:///sdcard/notification/ring.mp3");notification.sound = null;
10.设置震动提示,·使用默认振动notification.defaults |= Notification.DEFAULT_VIBRATE;·使用自定义振动0ms延迟后震动100ms,停止200ms后震动300mslong[] vibrate = {0,100,200,300}; notification.vibrate = vibrate;
11.设置该通知的优先级PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX,根据意思应该可以理解是什么级别!notification.priority = Notification.PRIORITY_DEFAULT;
12.设置意图Intent intent = new Intent();Intent.setAction("com.security.PERMISSION_CONTROL");
13.获得PendingIntent PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0); notification.SetLatestEventInfo(context, titStr, summStr, pendingIntent); notificationManager.notify(notificationID, notification);
这样完成了Notification的操作!另外5.0有浮动通知Heads-up notifications大家可以尝试使用一下,其实我并不不知道它是什么时候增加的!