版权声明:博主原创文章,转载注明出处! https://blog.csdn.net/xmh19936688/article/details/51703444
通知栏各种玩法
NotificationSample
网上各种玩法的demo
SimpleNotificationDemo
简单的通知实现
CustomNotificationDemo
自定义通知栏demo,实现自定义view与动态更新通知内容
知识点
Notification
notification.bigContentView = view;
这么做可以自定义通知的高度,但最大为200,最小为普通通知高度。如果使用builder的话,此处view与builder的一致。
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)
自定义通知的viewsetContentInfo(CharSequence info)
显示在右边的文本(时间下面)setNumber(int number)
显示在右边的数字(时间下面,如果调用了setContentInfo,则此设置不生效)setContentIntent(PendingIntent intent)
设置点击通知后的intentsetDeleteIntent(PendingIntent intent)
设置用户直接移除该通知后的intentsetFullScreenIntent(PendingIntent intent, boolean highPriority)
An intent to launch instead of posting the notification to the status barsetLargeIcon(Bitmap icon)
显示在通知栏的iconsetSmallIcon (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)
如果该通知正在显示,则再发通知不会触发铃声,震动,tickersetPriority(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)
发通知时在系统栏显示的文本和viewsetTicker (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的intentgetBroadcast()
封装一个发广播的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");