Android -- 再来一发Notification
之前写过一个Notificaiton的文章,用上面的方式去操作也是OK的,但是到后面的SDK之后,有些方法被弃用,甚至我到SDK23的时候,我发现有些方法直接没了,所以在这里重新写一下最新的用法。
http://www.cnblogs.com/yydcdut/p/3828941.html
步骤
- 创建一个通知栏的Builder构造类
- 定义通知栏的Action
- 设置通知栏点击事件
- 通知
代码
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
otificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("测试标题")//设置通知栏标题
.setContentText("测试内容") //设置通知栏显示内容</span>
.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //设置通知栏点击意图
// .setNumber(number) //设置通知集合的数量
.setTicker("测试通知来啦") //通知首次出现在通知栏,带上升动画效果的
.setWhen(System.currentTimeMillis())//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setPriority(Notification.PRIORITY_DEFAULT) //设置该通知优先级
// .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
.setOngoing(false)//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
//Notification.DEFAULT_ALL Notification.DEFAULT_SOUND 添加声音 // requires VIBRATE permission
.setSmallIcon(R.drawable.ic_launcher);
对应的各个方法的属性
Flags
功能:提醒标志符,向通知添加声音、闪灯和振动效果等设置达到通知提醒效果,可以组合多个属性
有2种设置方法:
实例化通知栏之后通过给他添加.flags属性赋值。
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
通过setContentIntent(PendingIntent intent)方法中的意图设置对应的flags
public PendingIntent getDefalutIntent(int flags){
PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);
return pendingIntent;
}
提醒标志符成员
Notification.FLAG_SHOW_LIGHTS //三色灯提醒,在使用三色灯提醒时候必须加该标志符
Notification.FLAG_ONGOING_EVENT //发起正在运行事件(活动中)
Notification.FLAG_INSISTENT //让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE //发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL //用户单击通知后自动消失
Notification.FLAG_NO_CLEAR //只有全部清除时,Notification才会清除 ,不清楚该通知(QQ的通知无法清除,就是用的这个)
Notification.FLAG_FOREGROUND_SERVICE //表示正在运行的服务
setDefaults(int defaults)
NotificationCompat.Builder中的方法,用于提示。
功能:向通知添加声音、闪灯和振动效果的最简单、使用默认(defaults)属性,可以组合多个属性(和方法1中提示效果一样的)
Notification.DEFAULT_VIBRATE //添加默认震动提醒 需要 VIBRATE permission
Notification.DEFAULT_SOUND // 添加默认声音提醒
Notification.DEFAULT_LIGHTS// 添加默认三色灯提醒
Notification.DEFAULT_ALL// 添加默认以上3种全部提醒
setVibrate(long[] pattern)
.setVibrate(new long[] {0,300,500,700});
//或者
mBuilder.build().vibrate = new long[] {0,300,500,700};
setLights(intledARGB ,intledOnMS ,intledOffMS )
功能:android支持三色灯提醒,这个方法就是设置不同场景下的不同颜色的灯。
描述:其中ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间。
注意:1)只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持三色灯提醒。2)这边的颜色跟设备有关,不是所有的颜色都可以,要看具体设备。
.setLights(0xff0000ff, 300, 0)
Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = 0xff0000ff;
notify.ledOnMS = 300;
notify.ledOffMS = 300;
setSound(Uri sound)
//获取默认铃声
.setDefaults(Notification.DEFAULT_SOUND)
//获取自定义铃声
.setSound(Uri.parse("file:///sdcard/xx/xx.mp3"))
//获取Android多媒体库内的铃声
.setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "5"))
setOngoing(boolean ongoing)
功能:设置为ture,表示它为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
setProgress(int max, int progress,boolean indeterminate)
属性:max:进度条最大数值 、progress:当前进度、indeterminate:表示进度是否不确定,true为不确定,如下第3幅图所示 ,false为确定下第1幅图所示
注意:此方法在4.0及以后版本才有用,如果为早期版本:需要自定义通知布局,其中包含ProgressBar视图
使用:如果为确定的进度条:调用setProgress(max, progress, false)来设置通知,在更新进度的时候在此发起通知更新progress,并且在下载完成后要移除进度条,通过调用setProgress(0, 0, false)既可。
如果为不确定(持续活动)的进度条,这是在处理进度无法准确获知时显示活动正在持续,所以调用setProgress(0, 0, true) ,操作结束时,调用setProgress(0, 0, false)并更新通知以移除指示条
自定义View
这里要用到RemoteViews这个类。
Notification的自定义布局是RemoteViews,和其他RemoteViews一样,在自定义视图布局文件中,仅支持FrameLayout、LinearLayout、RelativeLayout三种布局控件和AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper这些显示控件,不支持这些类的子类或Android提供的其他控件。否则会引起ClassNotFoundException异常。
public void showButtonNotify(){
NotificationCompat.Builder mBuilder = new Builder(this);
RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button);
mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon);
//API3.0 以上的时候显示按钮,否则消失
mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周杰伦");
mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七里香");
//如果版本号低于(3。0),那么不显示按钮
if(BaseTools.getSystemVersion() <= 9){
mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);
}else{
mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);
}
//
if(isPlay){
mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);
}else{
mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);
}
//点击的事件处理
Intent buttonIntent = new Intent(ACTION_BUTTON);
/* 上一首按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);
//这里加了广播,所及INTENT的必须用getBroadcast方法
PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);
/* 播放/暂停 按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
PendingIntent intent_paly = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_paly);
/* 下一首 按钮 */
buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);
PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);
mBuilder.setContent(mRemoteViews)
.setContentIntent(getDefalutIntent(Notification.FLAG_ONGOING_EVENT))
.setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示
.setTicker("正在播放")
.setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级
.setOngoing(true)
.setSmallIcon(R.drawable.sing_icon);
Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(notifyId, notify);
}
大视图风格通知
注:4.1之前的版本不支持大视图
详情区域根据用途可有多种风格:
- NotificationCompat.BigPictureStyle 大图片风格:详情区域包含一个256dp高度的位图
- NotificationCompat.BigTextStyle 大文字风格:显示一个大的文字块
- NotificationCompat.InboxStyle 收件箱风格:显示多行文字
NotificationCompat.BigPictureStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[5];
// Sets a title for the Inbox style big view
inboxStyle.setBigContentTitle("大视图内容:");
// Moves events into the big view
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
mBuilder.setContentTitle("测试标题")
.setContentText("测试内容")
// .setNumber(number)//显示数量
.setStyle(inboxStyle)//设置风格
.setTicker("测试通知来啦");
我是天王盖地虎的分割线
http://adchs.github.io/patterns/notifications.html
http://blog.csdn.net/vipzjyno1/article/details/25248021
Android -- 再来一发Notification的更多相关文章
- Android -- 再来一发Intent
之前写过一篇Intent的博客,主要说了一下隐式意图. 传送门:<Android -- Intent> Intent对象构成 Component name.Action.Data.Cate ...
- Android -- 再来一发Json
之前写过一篇Json的博客,当时用的是Gson包来解析的,这次来此自带的org.json来解析.传送门:<Gson解析复杂Json数据> JSON的结构 (1) Name/Value Pa ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- Android中使用Notification实现进度通知栏(Notification示例三)
我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能.实现效果如下: 在 ...
- Android中使用Notification实现宽视图通知栏(Notification示例二)
Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...
- Android中使用Notification实现普通通知栏(Notification示例一)
Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...
- 关于Android中new Notification
目前 Android 已经不推荐使用下列方式创建 Notification实例: Notification notification = new Notification(R.drawable.ic_ ...
- 在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- android笔记:Notification通知的使用
通知(Notification),当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现. 发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后 ...
随机推荐
- 约数 求反素数bzoj1053 bzoj1257
//约数 /* 求n的正约数集合:试除法 复杂度:O(sqrt(n)) 原理:扫描[1,sqrt(N)],尝试d能否整除n,若能,则N/d也能 */ ],m=; ;i*i<=n;i++){ ){ ...
- 线程使用中常见的错误-“System.InvalidOperationException”线程间操作无效: 从不是创建控件“ ”的线程访问它。
“System.InvalidOperationException”类型的未经处理的异常在 System.Windows.Forms.dll 中发生 其他信息: 线程间操作无效: 从不是创建控件“la ...
- 2018-2019-2 网络对抗week1 Kali安装 20165333陈国超
Kali安装 安装过程是按照网上的教程装的,链接点击[https://blog.csdn.net/KNIGH_YUN/article/details/79949512] 安装成功的截图 主要说一下安装 ...
- DDD领域模型数据访问权限之权限(十二)
实现权限的领域对象:BAS_Permission public partial class BAS_Permission:AggreateRoot { private IRepository<B ...
- 【C++ Primer 第10章】 10.4.1 插入迭代器
目录 • iostream迭代器 • 反向迭代器 插入迭代器 插入迭代器,这些迭代器被绑定到一个容器上,可以向容器插入元素. 头文件为:#include<iterator it=t 在it指 ...
- 在Centos中安装aria2c
# 安装aria2c 1 安装epel源 rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm ...
- 查看Windows系统里的进程已运行的时间
搜索 ProcessExplorer ,可以去微软下载它.右键点击项类,selcet conlumns...在 Process Performance 里 选择start time.有了进程的启动时间 ...
- java 使用反射在dto和entity 实体类之间进行转换
package com.example.demo.utils; import java.lang.reflect.Method; import java.util.List; import com.e ...
- Flume+Kafka整合
脚本生产数据---->flume采集数据----->kafka消费数据------->storm集群处理数据 日志文件使用log4j生成,滚动生成! 当前正在写入的文件在满足一定的数 ...
- python tkinter-容器、子窗体
Frame f = tkinter.Frame(width=380, height=270, bg='white').pack() LabelFrame f = tkinter.LabelFram ...