Notification使用基本步骤
1、得到NotificationManager:
String service = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =(NotificationManager)getSystemService(service)
2、实例化Notification对象
- Notification notification = new Notification();
3、设置Notification的各个属性:
- // 设置显示图标,该图标会在状态栏显示
- int icon = notification.icon = R.drawable.happy;
- // 设置显示提示信息,该信息也在状态栏显示
- String tickerText = "测试Notification";
- // 显示时间
- long when = System.currentTimeMillis(); notification.icon = icon;
- notification.tickerText = tickerText;
- notification.when = when;
- //也可以这样设置
- Notification notification_2=new Notification(icon,tickerText,when)
Notification属性
* 添加声音
notification.defaults |=Notification.DEFAULT_SOUND;
或者使用以下几种方式
notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
如果想要让声音持续重复直到用户对通知做出反应,则可以在notification的flags字段增加"FLAG_INSISTENT"
如果notification的defaults字段包括了"DEFAULT_SOUND"属性,则这个属性将覆盖sound字段中定义的声音
* 添加振动
notification.defaults |= Notification.DEFAULT_VIBRATE;
【说明】:加入手机震动,一定要在manifest.xml中加入权限:
<uses-permission android:name="android.permission.VIBRATE" />
或者可以定义自己的振动模式:
long[] vibrate = {0,100,200,300}; //0毫秒后开始振动,振动100毫秒后停止,再过200毫秒后再次振动300毫秒
notification.vibrate = vibrate;
long数组可以定义成想要的任何长度
如果notification的defaults字段包括了"DEFAULT_VIBRATE",则这个属性将覆盖vibrate字段中定义的振动
* 添加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;
//number字段表示此通知代表的当前事件数量,它将覆盖在状态栏图标的顶部,如果要使用此字段,必须从1开始
notification.iconLevel = ; //
* 常用字段:
contentIntent 设置PendingIntent对象,点击时发送该Intent
contentview 通知在状态栏的显示view,常与contentintent配合使用,点击该通知后,即触发contentintent
defaults 添加默认效果
flags 设置flag位,例如FLAG_NO_CLEAR等
icon 设置图标
sound 设置声音
tickerText 显示在状态栏中的文字
when 发送此通知的时间戳
P.S:
PendingIntent和Intent的区别:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.
4、发出通知
- //Notification标示ID
- private static final int ID = 1;
- //发出通知
- mNotificationManager.notify(ID, n);
通知显示有两种;
1、默认通知栏:具体使用是为notification对象设置setLatesEventInfo()方法(该方法内部创建了默认的RemoteViews对象,因此为默认显示)
2、自定义通知栏:使用自定义的View(RemoteViews对象)显示(功能更加自由,强大),具体方法为设置Notification对象的contentView 属性和contentIntent属性 ,此时不需要设置setLatestEventInfo()方法。具体使用方法如下:
Notification noti = new Notification(icon, title, when + 10000);
noti.flags = Notification.FLAG_INSISTENT;
// 1、创建一个自定义的消息布局 notification.xml
// 2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段
RemoteViews remoteView = new RemoteViews(this.getPackageName(),R.layout.notification);
remoteView.setImageViewResource(R.id.image, R.drawable.icon);
remoteView.setTextViewText(R.id.text , "Hello,this message is in a custom expanded view");
noti.contentView = remoteView;
// 3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)
//这儿点击后简答启动Settings模块
PendingIntent contentIntent = PendingIntent.getActivity
(MainActivity.this, 0,new Intent("android.settings.SETTINGS"), 0);
noti.contentIntent = contentIntent;