Android Notification

flag

FLAG_AUTO_CANCEL用户点击时消失
FLAG_FOREGROUND_SERVICE表示正在运行一个服务
FLAG_INSISTENT通知铃声会重复响直到通知被取消或通知窗口被打开
FLAG_NO_CLEAR当用户点击清除所有时该通知不被清除
FLAG_ONGOING_EVENT该通知被正在运行的事件所引用(比如正在通话)
FLAG_ONLY_ALERT_ONCE该通知仅首次发送时震动或播放通知铃声,之前发送的该通知未取消的情况下
FLAG_SHOW_LIGHTS发送该通知时打开呼吸灯
default

DEFAULT_SOUND使用默认通知声音
DEFAULT_VIBRATE使用默认震动效果
DEFAULT_LIGHTS使用默认通知呼吸灯
DEFAULT_ALL全部使用默认
NotificationManager

NotificationCompat.Builder

setAutoCancel(boolean autoCancel)是否用户点击后消失
setContent(RemoteViews views)自定义通知的view
setContentInfo(CharSequence info)显示在右边的文本(时间下面)
setNumber(int number)显示在右边的数字(时间下面,如果调用了setContentInfo,则此设置不生效)
setContentIntent(PendingIntent intent)设置点击通知后的intent
setDeleteIntent(PendingIntent intent)设置用户直接移除该通知后的intent
setFullScreenIntent(PendingIntent intent, boolean highPriority)An intent to launch instead of posting the notification to the status bar
setLargeIcon(Bitmap icon)显示在通知栏的icon
setSmallIcon (int icon, int level)显示在通知栏的icon,尺寸较小
setContentTitle(CharSequence title)通知标题,icon的右边上部
setContentText(CharSequence text)通知文本,icon的右边下部
setLights(int argb, int onMs, int offMs)设置呼吸灯,依次为argb色值,亮起持续毫秒数,熄灭持续毫秒数
setOngoing(boolean ongoing)如果是正在进行中的通知,则不能被移除
setOnlyAlertOnce(boolean onlyAlertOnce)如果该通知正在显示,则再发通知不会触发铃声,震动,ticker
setPriority(int pri)优先级
setProgress(int max, int progress, boolean indeterminate)进度
setSound (Uri sound)通知铃声Uri,在默认音频流播放
setSound (Uri sound, int streamType)通知铃声Uri,在指定音频流播放
setStyle (NotificationCompat.Style style)设置通知样式
setSubText (CharSequence text)设置第三行文本(与progress冲突)
setTicker (CharSequence tickerText, RemoteViews views)发通知时在系统栏显示的文本和view
setTicker (CharSequence tickerText)发通知时在系统栏显示的文本
setUsesChronometer (boolean b)显示自动计时器
setVibrate (long[] pattern)设置震动效果
setWhen (long when)设置通知发送时间
addAction (int icon, CharSequence title, PendingIntent intent)在底部扩展一块位置显示这个按钮,最多三个
PendingIntent

对intent的包装
pendingintent可以保存创建intent时的context,即使当时的context不存在了,也能从pendingintent里的context执行intent。
getActivity()封装打开一个activity的intent,就像Context.startActivity()
getActivitys()封装打开一组activity的intent
getBroadcast()封装一个发广播的intent,就像Context.sendBroadcast()
getService()封装一个开启服务的intent,就像Context.startService()
RemoteViews

非UI线程

从layout文件进行inflate:new RemoteViews(packageName,R.layout.filename)
提供修改子view的简单方法:remoteViews.setTextViewText(R.id.view_name, "string");


Notification通知效果



核心代码:
--------------------------------------------------------


/**
* 最普通的通知效果
*/
private void showNotifyOnlyText() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setLargeIcon(mLargeIcon)
           .setContentTitle("我是只有文字效果的通知")
           .setContentText("我没有铃声、震动、呼吸灯,但我就是一个通知");
   mManager.notify(1, builder.build());
}


/**
* 展示有自定义铃声效果的通知
* 补充:使用系统自带的铃声效果:Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
*/
private void showNotifyWithRing() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是伴有铃声效果的通知")
           .setContentText("美妙么?安静听~")
           //调用系统默认响铃,设置此属性后setSound()会无效
           //.setDefaults(Notification.DEFAULT_SOUND)
           //调用系统多媒体裤内的铃声
           //.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"));
           //调用自己提供的铃声,位于 /res/values/raw 目录下
           .setSound(Uri.parse("android.resource://com.littlejie.notification/" + R.raw.sound));
   //另一种设置铃声的方法
   //Notification notify = builder.build();
   //调用系统默认铃声
   //notify.defaults = Notification.DEFAULT_SOUND;
   //调用自己提供的铃声
   //notify.sound = Uri.parse("android.resource://com.littlejie.notification/"+R.raw.sound);
   //调用系统自带的铃声
   //notify.sound = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2");
   //mManager.notify(2,notify);
   mManager.notify(2, builder.build());
}


/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
* <uses-permission android:name="android.permission.VIBRATE" />
* 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
*/
private void showNotifyWithVibrate() {
   //震动也有两种设置方法,与设置铃声一样,在此不再赘述
   long[] vibrate = new long[]{0, 500, 1000, 1500};
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是伴有震动效果的通知")
           .setContentText("颤抖吧,凡人~")
           //使用系统默认的震动参数,会与自定义的冲突
           //.setDefaults(Notification.DEFAULT_VIBRATE)
           //自定义震动效果
           .setVibrate(vibrate);
   //另一种设置震动的方法
   //Notification notify = builder.build();
   //调用系统默认震动
   //notify.defaults = Notification.DEFAULT_VIBRATE;
   //调用自己设置的震动
   //notify.vibrate = vibrate;
   //mManager.notify(3,notify);
   mManager.notify(3, builder.build());
}


/**
* 显示带有呼吸灯效果的通知,但是不知道为什么,自己这里测试没成功
*/
private void showNotifyWithLights() {
   final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是带有呼吸灯效果的通知")
           .setContentText("一闪一闪亮晶晶~")
           //ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
           .setLights(0xFF0000, 3000, 3000);
   Notification notify = builder.build();
   //只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
   notify.flags = Notification.FLAG_SHOW_LIGHTS;
   //设置lights参数的另一种方式
   //notify.ledARGB = 0xFF0000;
   //notify.ledOnMS = 500;
   //notify.ledOffMS = 5000;
   //使用handler延迟发送通知,因为连接usb时,呼吸灯一直会亮着
   Handler handler = new Handler();
   handler.postDelayed(new Runnable() {
       @Override
       public void run() {
           mManager.notify(4, builder.build());
       }
   }, 10000);
}


/**
* 显示带有默认铃声、震动、呼吸灯效果的通知
* 如需实现自定义效果,请参考前面三个例子
*/
private void showNotifyWithMixed() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是有铃声+震动+呼吸灯效果的通知")
           .setContentText("我是最棒的~")
           //等价于setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
           .setDefaults(Notification.DEFAULT_ALL);
   mManager.notify(5, builder.build());
}


/**
* 通知无限循环,直到用户取消或者打开通知栏(其实触摸就可以了),效果与FLAG_ONLY_ALERT_ONCE相反
* 注:这里没有给Notification设置PendingIntent,也就是说该通知无法响应,所以只能手动取消
*/
private void showInsistentNotify() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("我是一个死循环,除非你取消或者响应")
           .setContentText("啦啦啦~")
           .setDefaults(Notification.DEFAULT_ALL);
   Notification notify = builder.build();
   notify.flags |= Notification.FLAG_INSISTENT;
   mManager.notify(6, notify);
}


/**
* 通知只执行一次,与默认的效果一样
*/
private void showAlertOnceNotify() {
   NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
           .setSmallIcon(R.mipmap.ic_launcher)
           .setContentTitle("仔细看,我就执行一遍")
           .setContentText("好了,已经一遍了~")
           .setDefaults(Notification.DEFAULT_ALL);
   Notification notify = builder.build();
   notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
   mManager.notify(7, notify);
}


/**
* 清除所有通知
*/
private void clearNotify() {
   mManager.cancelAll();
}
---------------------------------------------------------


//设置 Notification 的 flags = FLAG_NO_CLEAR
        //FLAG_ONGOING_EVENT 表示该通知通知放置在正在运行,不能被手动清除,但能通过 cancel() 方法清除
        //等价于 builder.setOngoing(true);


//设置系统默认提醒效果,一旦设置默认提醒效果,则自定义的提醒效果会全部失效。具体可看源码
//添加默认震动效果,需要申请震动权限
//<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_VIBRATE


//添加系统默认声音效果,设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_SOUND


//添加默认呼吸灯效果,使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_LIGHTS


//添加上述三种默认提醒效果
Notification.DEFAULT_ALL


//提醒效果常用 Flag
//三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_SHOW_LIGHTS


//发起正在运行事件(活动中)
Notification.FLAG_ONGOING_EVENT


//让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_INSISTENT


//发起Notification后,铃声和震动均只执行一次
Notification.FLAG_ONLY_ALERT_ONCE


//用户单击通知后自动消失
Notification.FLAG_AUTO_CANCEL


//只有调用NotificationManager.cancel()时才会清除
Notification.FLAG_NO_CLEAR


//表示正在运行的服务
Notification.FLAG_FOREGROUND_SERVICE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值