Android Notification 基础工作学习 2010-06-27 17:42:36 阅读869 评论0 字号:大中小 订阅
在Android中,基本的Notification就是有事件发生的时候在屏幕顶端的Notification bar上显示一个图标。然后拉下Notification bar,点击Notification的项目,会调用相应的程序做处理。比如有新短信,就会出现短信的图标,拉下Notification bar,点击图标会调用短信查看程序。
我们先看一下Notification的Sample Code,然后逐行做解说,大致能了解它的基本构成。
import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;...private void showNotification(Message msg, int id) { NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.notiicon, msg .getTitle(), System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL; Intent intent = new Intent(this, MainActivity.class); Bundle bundle = new Bundle(); bundle.putString("info", msg.getInfo()); intent.putExtras(bundle); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent contentIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT); notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(), contentIntent); notiManager.notify(id, notification); }
首先导入三个类,Notification,NotificationManager,PendingIntent。 值得一提的是PendingIntent,它可以看做是Intent这封信的一个信封。PendingIntent基本上是Intent的包装和描述,对象收到PendingIntent后,可以得到其中的Intent再发出去。
NotificationManager notiManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);上面这一句,从系统中获得Notification服务,getSystemService()就是这么用,来获得系统服务的。
Notification notification = new Notification(R.drawable.notiicon, msg .getTitle(), System.currentTimeMillis()); notification.flags = Notification.FLAG_AUTO_CANCEL;
然后是构造一个Notification,包括三个属性,图标,图标后面的文字,以及Notification时间部分显示出来的时间,通常使用当前时间。FLAG_AUTO_CANCEL说明Notification点击一次就消失。
Intent intent = new Intent(this, MainActivity.class); Bundle bundle = new Bundle(); bundle.putString("info", msg.getInfo()); intent.putExtras(bundle); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
上面这部分Code,构造一个Intent,并且放进去一条信息。 FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_NEW_TASK者两个FLAG表示优先寻找已经打开的应用,如果应用没有打开那么启动它。
PendingIntent contentIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);这一句代码把Intent包装在PendingIntent里,this是Context,id是PendingIntent的标志,如果id相同会被认为是一个。FLAG_UPDATE_CURRENT是指后来的PendingIntent会更新前面的。
notification.setLatestEventInfo(this, msg.getTitle(), msg.getInfo(), contentIntent); notiManager.notify(id, notification);最后这两行添加状态栏的详细信息,包装PendingIntent给Notification,最后发送Notification。
分享到:
相关推荐
android notification完全解析Demo
主要利用了notification service 和receiver 实现了用户通知栏更新,失败了点击重新下载,下载好了点击自动安装,布局很丑,请根据自己项目的实际需求自行修改
Notification显示音乐下载的界面
类型:NotificationDemo 功能:从网上下载apk存储到手机指定目录,并在通知栏里显示其下载进度,如果下载错误,或者不想要该apk可以删除 涉及知识: 文件流、网络下载链接协议、读写权限、Handler、Notification、...
Android Notification 常见样式代码及发送demo,其中包含源码及一个app-release.apk,可直接发送固定样式,也可自定义样式。可以作为通知测试的小工具,也可作为参考demo。
这个项目整合了Android平台所有Notification的使用方法,有些使用场景不生效,不知道是否是我使用方法有问题。
Android Notification消息框 Toast弹出框用法演示范例,本例中关于 Toast弹出框的演示,演示了适时的 Toast和长时间的 Toast,关于Notification的定义,则演示了高级Notification的用法,自定义4种Notification的...
android Notification
android notification 软件更新
android notification Service 文件下载
android 利用notification 通知栏 实现动态下载.
Android notification进度条 demo,欢迎下载!!!!!!!!!!!!!!!!!!!!!!!!!!!!
android notification 通知 源码 例子
Android NOtification 使用Android NOtification 使用
Convert JSON to Android Notification. This is very useful for push notification (GCM). Usage Notification notification = Json2Notification.from(context).with(json).notification(); NotificationManager ...
简单介绍Android Notification用法 详细介绍NotificationManageService和StatusBarManageService与App层交互过程
了解Android Notification的基本用法
Android Notification通知 简单使用 demo下载。 解决问题:兼容高低版本,帮助用户打开通知开关,android5.x手机状态栏icon是白块问题。 详细文章请参考 ...
各种各样的android Notification效果
kinds of notification