Android API Level在11前后及16之后时Notification的不同用法
作为刚入门Android的小白,最近在按照郭大神的《第一行代码》在练习,在用到Notification时遇到了一些问题,网上资料比较零散,我这里做了一个总结分析给各位,若有错误,恳请指正~
Notification是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。
Notification支持文字内容显示、震动、三色灯、铃声等多种提示形式,在默认情况下,Notification仅显示消息标题、消息内容、送达时间这3项内容。
以下就是通知的基本布局:

不同API level的区别主要是Notification的构造方法、得到实例的方法,这里顺便总结一下Notification的用法,按照步骤分别给出不同API level下的做法:
1、获取Notification管理器
NotificationManager noteManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
获取管理器的方式都一样,没有改变。
2、新建一个Notification,设置状态栏显示样式
这里只列举了最简单的样式设置,具体的大家可以参考API文档。
// API Level < 11 (Android 3.0)
Notification notification = new Notification(
R.mipmap.ic_launcher, "This is ticker text",
System.currentTimeMillis());
API<11时,可以直接用Notification的构造方法新建一个Notification,十分方便。
ps:我用的IDE是AndroidStudio,所以icon的id是R.mipmap.***。
// API Level >= 11 (Android 3.0) && API Level < 16 (Android 4.1)
Notification.Builder builder = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("This is ticker text")
.setWhen(System.currentTimeMillis());
Notification note = builder.getNotification(); // 调用getNotification()来生成Notification
API>11后,就要用Notification.Builder()来代替了,官方API是这样说的:
Notification(int icon, CharSequence tickerText, long when)
Notification.Builder instead.NotificationCompat.Builder, available in the Android Support library.//API Level >= 4 (Android 1.6) && API Level < 16 (Android 4.1)
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("This is ticker text")
.setWhen(System.currentTimeMillis());
Notification note =builder.getNotification(); //调用builder.getNotification()来生成Notification
因此推荐用NotificationCompat.Builder这种方式。
注意到我在注释里写的 "&& API Level < 16 (Android 4.1)" 了吗?这是因为在Google官方API文档上是这么说的:
public Notification getNotification () Added in API level 11
This method was deprecated in API level .
Use build() instead
因此,当API>=16即Android4.1之后,就要用build()来代替了,使用方法一样,在此就不赘述了。
3、设置Notification的触发事件
点击Notification后,一般都是触发一个新的Activity:
Intent intent = new Intent(this, AnotherActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
这里不同API版本都一样。
4、设置Notification在通知栏里的样式
// API Level < 11 (Android 3.0)
note.setLatestEventInfo(this, "This is content title", "This is content text", pi);
API<11时,调用的是Notification类的setLatestEventInfo方法。
// API Level >= 11 (Android 3.0)
builder.setContentIntent(pi)
.setContentTitle("This is content title")
.setContentText("This is content text");
而当API>=11时,设置通知栏中的样式是调用Builder的setContentIntent方法,设置好之后在调用build()方法生成Notification实例。
类似的,NotificationCompat.Builder也是调用同样的方法,这里就不赘述了。
5、发布该Notification
第一个参数为该notification的ID
noteManager.notify(1, note);
总结一下:
低版本(API低于11、16)中的部分方法已经被弃用:
(1)Notification.Builder(this).getNotification()
(2)mNotification.setLatestEventInfo(this, "title", "content", null);
建议开发过程中尽量使用NotificationCompat.Builder(this)的构建方法去创建一个通知类。
Android API Level在11前后及16之后时Notification的不同用法的更多相关文章
- Call requires API level 21(Current min is 16)
Call requires API level 21(Current min is 16) Android开发中,遇到类似这种问题,如何处理? 一种办法是提升sdk最低版本到21,在Android s ...
- Android api level对照表
转自:blog.csdn.net/lihenair/article/details/49869299 Platform Version API Level VERSION_CODE Notes And ...
- Android API level 版本对应关系
详情地址:http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API L ...
- Android API level 与version对应关系
https://www.cnblogs.com/jinglecode/p/7753107.html Platform Version API Level VERSION_CODE 中文名称 Andro ...
- android api level对应表(copy)
Platform Version API Level VERSION_CODE Notes Android 4.4 19 KITKAT Platform Highlights Android 4.3 ...
- Android API Level与sdk版本对照表
API等级1: Android 1.0 API等级2: Android 1.1 Petit Four 花式小蛋糕 API等级3: Android 1.5 Cupcake 纸杯蛋糕 API等级4: An ...
- Android API Guides 学习笔记---Application Fundamentals(一)
今天开始学习google官网上的API guides ,主要读了Application Fundamentals这一章节,此章节介绍了一个App的基本组成,共包括四大部分内容. 1. App ...
- Android Call requires API level 11 (current min is 8)的解决方案
[错误描述] 在用Eclipse开发过程中,为了兼容Android2.2和4.0以上版本,我在使用Notification类时做了2个版本的代码,代码根据系统版本不同执行相应模块,结果,等我输完代码, ...
- Android版本和API Level对应关系
http://developer.android.com/guide/topics/manifest/uses-sdk-element.html Platform Version API ...
随机推荐
- OpenSuSE zypper repo及Desktop媒体播放器设置 for OpenSuSE12.
1.禁用官方源和DVD光盘源,启用中国大陆源 使用DVD光盘安装好openSUSE 12.2之后,软件安装源中默认存在一个名称为”openSUSE-12.2-1.6″的软件源,这个源的URL实际上是指 ...
- 北京西服定做_衬衫定制_关于我们_Dimoon TLR.
北京西服定做_衬衫定制_关于我们_Dimoon TLR.
- Mule与其它web应用服务器的区别
跟JBoss.Tomcat或其它web应用服务器相比,Mule有何不同?虽然他们有一些重要的相同点,不同点可以归结为你想达到的目标是什么.某些种类的应用对于Mule来说比较容易去编写.部署和管理,其它 ...
- optimizer for eclipse--Eclipse优化,让你的Eclipse快来飞!
官方网站:http://zeroturnaround.com/free/optimizer-for-eclipse/ infoq网址:http://www.infoq.com/cn/news/2015 ...
- DevExpress GridView使用技巧之列标题点击事件
在这里使用GridView的MouseDown事件.这里同样使用的是GridHitInfo来获取点击位置的信息,来判断是否在列标题上.GridHitInfo根据鼠标点击的x.y坐标获取该点的相关信息, ...
- js触摸屏案例
js 手机端触发事事件.javascript手机端/移动端触发事件 处理Touch事件能让你跟踪用户的每一根手指的位置.你可以绑定以下四种Touch事件: 1 2 3 4 touchstart: ...
- Parallel多线程
随着多核时代的到来,并行开发越来越展示出它的强大威力!使用并行程序,充分的利用系统资源,提高程序的性能.在.net 4.0中,微软给我们提供了一个新的命名空间:System.Threading.Tas ...
- js 记忆函数
使用js记忆函数来计算菲波那切数列.阶乘等,可以极大减少我们必须要做的工作,加速程序计算. 1.编写记忆函数memoizer var memoizer = function(memo, fundame ...
- Oracle事物基础
事务 1 事务定义 数据库事务是SQL语句的组合作为一个"工作单元".要么全部完成,要么全部不做. 每个事务都有一个开始和一个结束. 2 事务开始 1. 你连接到数据库并执行DML ...
- DropdownList的处理总结
创建一: List<SelectListItem> items = new List<SelectListItem>() { new SelectListItem(){Text ...