android 之 Notification
*********** Notification and NotificationManager *********
在使用手机时,当有未接来电或是短消息时,手机会给出相应的提示信息,这些提示信息会显示到手机屏幕的状态栏上,android 也提供了处理这些信息的类,Notification and NotificationManager
ps:消息通知的组件:Toast和Notification(区别就不介绍了)
当有消息通知时,状态栏会显示通知的图标和文字,通过下拉状态栏,就可以看到消息了(比如短息,各种app 的广告推送等)
Notification:全局效果的通知
NotificationManager:用户发送Notification通知的系统服务
使用步骤:
1.调用getSystemService()方法获取系统的NotificationManager服务
2.创建一个Notification对象,并为其设置各种属性
3.为Notification对象设置事件信息
4.通过NotificationManager类的notify()方法发送Notification通知
*****
1.NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
2.Notification notification = new Notification(android.R.drawable.stat_sys_warning, "普通通知", System.currentTimeMillis());
3.//设定各种属性
// 设定声音
notification.defaults |= Notification.DEFAULT_SOUND;
//设定震动(需加VIBRATE权限)
notification.defaults |= Notification.DEFAULT_VIBRATE;
// 设定LED灯提醒
notification.defaults |= Notification.DEFAULT_LIGHTS;
// 设置点击此通知后自动清除
notification.flags |= Notification.FLAG_AUTO_CANCEL;
4.mNotificationManager.notify(NORMAL_NOTIFY_ID, notification);
ps:也可用build 来构造Notification
Notification.Builder notificationBuilder = new Notification.Builder(
context).setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true).setContentTitle(contentTitle)
.setContentText(contentText).setContentIntent(mContentIntent)
.setSound(soundURI).setVibrate(mVibratePattern);
Notification 常用的属性:
contentIntent:设置PendingIntent对象,点击时发送该Intent
defaults:添加默认效果
flags:设置flag位,比如FLAG_NO_CLEAR
icon:设置图标
sound:设置声音
ticketText:显示在状态栏中的文字
when:发送此邮件的时间戳
几种常用的flags:
1.FLAG_AUTO_CANCEL:在用户查看通知信息后自动关闭通知;
2.FLAG_INSISTENT:在用户响应之前一直重复;
3.FLAG_ONGOING_EVENT:放置在“正在运行”栏目中,表示程序正在运行,可见状态,或是后台运行;
4.FLAG_NO_CLEAR:查看通知后不会自动取消,对一直进行中的通知非常有用。
浙公网安备 33010602011771号