【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知
应用程序可以使用Notifications来通知用户某个事件发生了(如收到短信)。类NotificationManager 用来处理Notification, NotificationManager可以:
- 在Status Bar上显示一个新的图标。
- 在Extended status bar 窗口上显示附加信息或是启动一个Activity。
- 显示背光/LED。
- 使设备震动。
- 发出声音等。
对于一些没有UI的应用程序组件(如Broadcast Receiver, Services)或是非活动状态的Activity,Notification是推荐使用的可以提醒用户注意的方法。
Notification通常是在Status Bar上显示图标或是文字,此时用户如果想了解Notification的详细内容,可以按住Status Bar下拉显示Expanded Status bar 窗口,在Expanded Status bar窗口显示该Notification详情并可以启动对应的Activity。
IncomingMessage 示例介绍了Notification的一般用法:
1. 首先是取得NotificationManager 对象:
// look up the notification manager service
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
2. 然后创建Notification,创建Notification时指定显示在Status bar的图标,文字以及显示Notification的时间:
// construct the Notification object.
Notification notif = new Notification(R.drawable.stat_sample, tickerText,
System.currentTimeMillis());
3. 然后定义当用户打开Extented status windows窗口时的标题及详情。Notification常常代表了一个请求或者需要引起注意的事件,因此可以指定一个PendingIntent来响应用户点击这个Notification。
// The details of our fake message
CharSequence from = "Joe";
CharSequence message;
switch ((new Random().nextInt()) % 3) {
case 0: message = "r u hungry? i am starved"; break;
case 1: message = "im nearby u"; break;
default: message = "kthx. meet u for dinner. cul8r"; break;
} // The PendingIntent to launch our activity if the user selects this
// notification. Note the use of FLAG_CANCEL_CURRENT so that, if there
// is already an active matching pending intent, cancel it and replace
// it with the new array of Intents.
PendingIntent contentIntent = PendingIntent.getActivities(this, 0,
makeMessageIntentStack(this, from, message), PendingIntent.FLAG_CANCEL_CURRENT); // The ticker text, this uses a formatted string so our message could be localized
String tickerText = getString(R.string.imcoming_message_ticker_text, message); // construct the Notification object.
Notification notif = new Notification(R.drawable.stat_sample, tickerText,
System.currentTimeMillis()); // Set the info for the views that show in the notification panel.
notif.setLatestEventInfo(this, from, message, contentIntent); // We'll have this notification do the default sound, vibration, and led.
// Note that if you want any of these behaviors, you should always have
// a preference for the user to turn them off.
notif.defaults = Notification.DEFAULT_ALL;
4. 最后是触发这个Notification:
// Note that we use R.layout.incoming_message_panel as the ID for
// the notification. It could be any integer you want, but we use
// the convention of using a resource id for a string related to
// the notification. It will always be a unique number within your
// application.
nm.notify(R.string.imcoming_message_ticker_text, notif);
一般来说对应同一个事件可以使用同一个Notification来通知用户,nm.notify的第一个参数为Notification 的ID,类型为整数。 可以使用同一个ID来表示同一个Notification,也可以使用这个ID来取消这个Notification,在IncomingMessage 中当用户点击显示了这个IncomingMessage详情后,会取消这个Notification(类IncomingMessageView中)。
nm.cancel(R.string.imcoming_message_ticker_text);

【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知的更多相关文章
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
本文链接:[起航计划 002]2015 起航计划 Android APIDemo的魔鬼步伐 01 参考链接:http://blog.csdn.net/column/details/mapdigitap ...
- 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程
本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...
- 【起航计划 031】2015 起航计划 Android APIDemo的魔鬼步伐 30 App->Preferences->Advanced preferences 自定义preference OnPreferenceChangeListener
前篇文章Android ApiDemo示例解析(31):App->Preferences->Launching preferences 中用到了Advanced preferences 中 ...
- 【起航计划 027】2015 起航计划 Android APIDemo的魔鬼步伐 26 App->Preferences->Preferences from XML 偏好设置界面
我们在前面的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 介绍了可以使用Shared Preferences来存储一些状 ...
- 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的UI类似,但 ...
- 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考
01 API Demos ApiDemos 详细介绍了Android平台主要的 API,android 5.0主要包括下图几个大类,涵盖了数百api示例:
- 【起航计划 035】2015 起航计划 Android APIDemo的魔鬼步伐 34 App->Service->Local Service Controller
Local Service Controller 是将LocalService当作“Started”Service来使用,相对于”Bound” Service 来说,这种模式用法要简单得多,Local ...
- 【起航计划 034】2015 起航计划 Android APIDemo的魔鬼步伐 33 App->Service->Local Service Binding 绑定服务 ServiceConnection Binder
本例和下列Local Service Controller 的Activity代码都定义在LocalServiceActivities.Java 中,作为LocalServiceActivities ...
随机推荐
- 最短路【bzoj2464】: 中山市选[2009]小明的游戏
2464: 中山市选[2009]小明的游戏 Description 小明最近喜欢玩一个游戏.给定一个n * m的棋盘,上面有两种格子#和@.游戏的规则很简单:给定一个起始位置和一个目标位置,小明每一步 ...
- HiBench学习笔记
HiBench资源及教程:https://github.com/intel-hadoop/HiBench 介绍: Hadoop新人 环境 linux 该笔记针对英语弱鸡&自己在使用过程用遇到的 ...
- kuangbin专题七 POJ3264 Balanced Lineup (线段树最大最小)
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...
- tinkphp中的自动验证
tinkphp是国内非常流行的一个开源框架,国内大小公司都在用的框架.对于初学的好多同学感觉不太好上手,其实并没没有大家想的那么复杂.自动验证功能是thinkphp提高的一种数据验证方法,分为动态和静 ...
- shiro 的简单应用
shiro 的简单应用 shiro官网:https://shiro.apache.org/ shiro 简介: Apache Shiro(日语"堡垒(Castle)"的意思)是 ...
- [Groovy]转:Groovy 通过 isCase 方法进行分类
闭包实现了 isCase 方法,这样闭包可以在 grep 和 sw itch 中作为分类器使用,在这种情况下,各自的参数传递给闭包,然后调用闭包进行计算得到一个 Boo lean 值(参考 6.1 节 ...
- SprimgMVC学习笔记(三)—— 参数绑定
一.默认支持的参数类型 1.1 需求 打开商品编辑页面,展示商品信息. 1.2 需求分析 编辑商品信息,首先要显示商品详情 需要根据商品id查询商品信息,然后展示到页面. 请求的url:/itemEd ...
- python3 发送QQ邮件
from email.header import Headerfrom email.mime.text import MIMETextfrom smtplib import SMTP_SSL emai ...
- python3 continue和break 区别
for i in range(10): if i==5: continue #跳出当次循环 if i==8: break #跳出整个for循环 print(i)
- C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
由于最近经常使用到c语言中的读写文件,所以在此总结以下,方便以后查找. 在c中,文件操作都是由库函数来实现的,主要是分为读和写两种操作,以下详细讲解以下所有有关文件操作的邯郸乎的用法: //C++写入 ...