android Notification 的使用(锁定通知栏)
近期一直在研究 android 。并一边研究一边做应用。当中遇到了把程序通知常驻在 Notification 栏,而且不能被 clear 掉(就像android QQ一样)的问题。经过研究实现了其功能。现把 Notification 的使用总结例如以下:
Notification 的使用须要导入 3 个类
1
2
3
|
import android.app.PendingIntent; import android.app.NotificationManager; import android.app.Notification; |
代码演示样例及说明
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
NotificationManager Notification new Notification(R.drawable.chat, "Hello,there!" , n.flags Intent new Intent(arg0.getContext(), class ); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); //PendingIntent PendingIntent arg0.getContext(), R.string.app_name, i, PendingIntent.FLAG_UPDATE_CURRENT); n.setLatestEventInfo( arg0.getContext(), "Hello,there!" , "Hello,there,I'm , contentIntent); nm.notify(R.string.app_name, |
以下依次对每一段代码进行分析:
1
|
NotificationManager |
创建 NotificationManager。当中创建的 nm 对象负责“发出”与“取消” Notification。
1
2
|
Notification new Notification(R.drawable.chat, "Hello,there!" , n.flags |
创建 Notification ,參数依次为:icon的资源id,在状态栏上展示的滚动信息。时间。当中创建的 n 对象用来描写叙述出如今系统通知栏的信息。之后我们将会看到会在 n 对象上设置点击此条通知发出的Intent。
1
|
n.flags |
设置 n.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后。可以清除该通知。
1
2
|
Intent new Intent(arg0.getContext(), class ); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); |
创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent
请注意,假设要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。
Intent.FLAG_ACTIVITY_CLEAR_TOP :假设在当前Task中,有要启动的Activity。那么把该Acitivity之前的全部Activity都关掉,并把此Activity置前以避免创建Activity的实例
Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前全部已创建的Task中是否有该要启动的Activity的Task,若有。则在该Task上创建Activity,若没有则新建具有该Activity属性的Task。并在该新建的Task上创建Activity。很多其它请參见 “ (转载)Android下Affinities和Task ”
1
2
3
4
5
6
|
//PendingIntent PendingIntent arg0.getContext(), R.string.app_name, i, PendingIntent.FLAG_UPDATE_CURRENT); |
PendingIntent 为Intent的包装。这里是启动Intent的描写叙述。PendingIntent.getActivity 返回的PendingIntent表示。此PendingIntent实例中的Intent是用于启动 Activity 的Intent。PendingIntent.getActivity的參数依次为:Context。发送者的请求码(能够填0),用于系统发送的Intent,标志位。
当中 PendingIntent.FLAG_UPDATE_CURRENT 表示假设该描写叙述的PendingIntent已存在。则改变已存在的PendingIntent的Extra数据为新的PendingIntent的Extra数据。
这里再简要说一下 Intent 与 PendingIntent 的差别:
Intent :意图,即告诉系统我要干什么。然后系统依据这个Intent做相应的事。如startActivity相当于发送消息,而Intent是消息的内容。
PendingIntent :包装Intent,Intent 是我们直接使用 startActivity , startService 或 sendBroadcast 启动某项工作的意图。而某些时候,我们并不能直接调用startActivity , startServide 或 sendBroadcast ,而是当程序或系统达到某一条件才发送Intent。如这里的Notification,当用户点击Notification之后,由系统发出一条Activity 的 Intent 。因此假设我们不用某种方法来告诉系统的话。系统是不知道是使用
startActivity ,startService 还是 sendBroadcast 来启动Intent 的(当然还有其它的“描写叙述”),因此这里便须要PendingIntent。
1
2
3
4
5
|
n.setLatestEventInfo( arg0.getContext(), "Hello,there!" , "Hello,there,I'm , contentIntent); |
设置显示在通知下拉框中的信息,參数依次为:Context,标题。内容,PendingIntent。
1
|
nm.notify(R.string.app_name, |
启动Notification,參数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation。假设程序中仅仅有一个Notification那么这里随便你填什么都能够,只是类型必需要为int),要通知的Notification。
怎样使自己的Notification像Android QQ一样能出如今 “正在执行的”栏目以下
事实上非常easy,仅仅需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便能够了。
怎样改变 Notification 在“正在执行的”栏目以下的布局
创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便能够了。如:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
PendingIntent arg0.getContext(), R.string.app_name, i, PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews new RemoteViews(Main. this .getPackageName(), rv.setImageViewResource(R.id.image, rv.setTextViewText(R.id.text, "Hello,there,I'm ); n.contentView n.contentIntent nm.notify(R.string.app_name, |
注意,假设使用了contentView。那么便不要使用Notification.setLatestEventInfo。假设setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖。显示的便是 setLatestEventInfo 的效果。假设 setLatestEventInfo
在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说无论你想要setLatestEventInfo 或 contentView 的自己定义效果,请保证始终仅仅有一句设置代码,由于在最后一句绑定的时候。之前的设置contentView或setLatestEventInfo的代码都是全然没有必要的。
最新文章
- Android 照相 滤镜
- [nRF51822] 6、基于nRF51822平台的flash读写研究
- sin, miss the mark, correct our aim and try again
- 1-MSP430点亮一个灯
- 关于C++对汉字拼音的处理
- 监控持有sql和被堵塞的sql
- ios系统下的QQ浏览器jquert的BUG
- T-SQL编程语句
- Order Management Useful Scripts
- setTimeout循环传参数几种方式
- 在Bootstrap开发框架中使用bootstrapTable表格插件和jstree树形列表插件时候,对树列表条件和查询条件的处理
- react_app 项目开发 (8)_角色管理_用户管理----权限管理 ---- shouldComponentUpdate
- Zynq PS和PL间的连接
- 微信小程序是怎么运行的?
- mySql的UDF是什么
- python之tkinter使用-文件系统遍历
- 关于Laravel 迁移数据库的问题
- 【黑客免杀攻防】读书笔记8 - 软件逆向工程基础2(if-else,三目运算符)
- Java Swing类 颜色、按键状态判断例子代码
- u3d fpsCounter
热门文章
- 理解内存----优化SQL Server内存配置
- python 中的 sorted
- Spring Bean Life Cycle Methods – InitializingBean, DisposableBean, @PostConstruct, @PreDestroy and *Aware interfaces
- Exception thrown in catch and finally clause
- jquery 获取table当前行值
- HTML5 Canvas 画钟表
- 【高级功能】使用 Ajax
- Android 在同一个手机上安装多个同样的apk,便于调试
- 可移动磁盘显示0kb打不开怎么办
- MySql修改root密码、设置IP访问