android notification,notificationmanager详解
我们知道在使用Android的通知的时候一定会用到NotificationManager 、 Notification这两个类,这两个类的作用分别是:
NotificationManager : 是状态栏通知的管理类,负责发通知、清楚通知等。
Notification:状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。
这里需要声明一点,由于Android的系统升级,Android在通知这块也有很多老的东西被抛弃了,一个是api11的版本,一个是api16的版本。我们来比较下api11之前的用法这是通用的:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // 下面需兼容Android 2.x版本是的处理方式 Notification notify1 = new Notification(); notify1.icon = R.drawable.message; notify1.tickerText = "TickerText:您有新短消息,请注意查收!"; notify1.when = System.currentTimeMillis(); notify1.setLatestEventInfo(this, "Notification Title", "This is the notification message", pendingIntent); notify1.number = 1; notify1.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(NOTIFICATION_FLAG, notify1);
api11-api16的用法是这样的(主要是新增了自定义通知图标,并且通知的构造方式也发生了改变)
PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // API11之后才支持 Notification notify2 = new Notification.Builder(this) .setSmallIcon(R.drawable.message) .setTicker("TickerText:" + "您有新短消息,请注意查收!") .setContentTitle("Notification Title") .setContentText("This is the notification message") .setContentIntent(pendingIntent2) .setNumber(1) .getNotification(); // 需要注意build()是在API level // 16及之后增加的,在API11中可以使用getNotificatin()来代替 notify2.flags |= Notification.FLAG_AUTO_CANCEL; manager.notify(NOTIFICATION_FLAG, notify2);
api16之后
PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0); // API16之后才支持 Notification notify3 = new Notification.Builder(this) .setSmallIcon(R.drawable.message) .setTicker("TickerText:" + "您有新短消息,请注意查收!") .setContentTitle("Notification Title") .setContentText("This is the notification message") .setContentIntent(pendingIntent3).setNumber(1).build(); notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。 manager.notify(NOTIFICATION_FLAG, notify3);//关联通知
我们这里讲的主要是api16之后的使用方法
首先我们通过系统的Service获取NotificationManager对象,然后通过他将消息发送给系统,获取方法如下:
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification主要包含以下参数:
- An icon (通知的图标)
- A title and expanded message (通知的标题和内容)
- A
PendingIntent (点击通知执行页面跳转)
使用流程:
1、创建NotificationManager
通过NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);获取NotificationNotificationManager 消息管理类,
2,创建Notification实体
通过Notification.Builder builder = new Notification.Builder(this);创建一个通知的实体,里面可以包含很多的参数,如通知的Icon,消息内容,跳转等。
3,通过notificationManager.notify(0, builder.build());将消息绑定,里面会用到NotificationService(这里不做讲解)
普通通知
Notification.Builder builder = new Notification.Builder(this); Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/xiangzhihong8")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0); builder.setContentIntent(pendingIntent); builder.setSmallIcon(R.drawable.lanucher); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher)); builder.setAutoCancel(true); builder.setContentTitle("普通通知"); builder.setContentText("您有新短消息,请注意查收"); notificationManager.notify(0, builder.build());
折叠式通知
我们还可以通过RemoteViews(这里就是桌面小控件的实现,不知道大家是否还有印象)
Notification.Builder builder = new Notification.Builder(this); Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/itachi85/")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0); builder.setContentIntent(pendingIntent); builder.setSmallIcon(R.drawable.foldleft); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.lanucher)); builder.setAutoCancel(true); builder.setContentTitle("折叠菜单"); builder.setContentText("您有新短消息,请注意查收"); //用RemoteViews来创建自定义Notification视图 RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.view_fold); Notification notification = builder.build(); //指定展开时的视图 notification.bigContentView = remoteViews; notificationManager.notify(1, notification);
自定义通知
Notification.Builder builder = new Notification.Builder(this); Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/xiangzhihong8")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0); builder.setContentIntent(pendingIntent); builder.setSmallIcon(R.drawable.foldleft); builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon)); builder.setAutoCancel(true); builder.setContentTitle("自定义菜单"); builder.setContentText("您有新短消息,请注意查收"); //设置点击跳转 Intent hangIntent = new Intent(this,NotificationActivity.class); hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent hangPendingIntent = PendingIntent.getActivity(this, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setFullScreenIntent(hangPendingIntent, true); notificationManager.notify(2, builder.build());
源码:http://download.csdn.net/detail/xiangzhihong8/9639345
android notification,notificationmanager详解的更多相关文章
- Android Notification通知详解
根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时 ...
- Android Studio 插件开发详解二:工具类
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...
- android:ToolBar详解
android:ToolBar详解(手把手教程) 泡在网上的日子 发表于 2014-11-18 12:49 第 124857 次阅读 ToolBar 42 来源 http://blog.mosil.b ...
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
- 【转】Android Canvas绘图详解(图文)
转自:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html Android Canvas绘图详解(图文) 泡 ...
- Android 核心分析 之八Android 启动过程详解
Android 启动过程详解 Android从Linux系统启动有4个步骤: (1) init进程启动 (2) Native服务启动 (3) System Server,Android服务启动 (4) ...
- Android GLSurfaceView用法详解(二)
输入如何处理 若是开发一个交互型的应用(如游戏),通常需要子类化 GLSurfaceView,由此可以获取输入事件.下面有个例子: java代码: package eoe.ClearTes ...
- Android编译过程详解(一)
Android编译过程详解(一) 注:本文转载自Android编译过程详解(一):http://www.cnblogs.com/mr-raptor/archive/2012/06/07/2540359 ...
- android屏幕适配详解
android屏幕适配详解 官方地址:http://developer.android.com/guide/practices/screens_support.html 一.关于布局适配建议 1.不要 ...
- Android.mk文件详解(转)
源:Android.mk文件详解 从对Makefile一无所知开始,折腾了一个多星期,终于对Android.mk有了一个全面些的了解.了解了标准的Makefile后,发现Android.mk其实是把真 ...
随机推荐
- PTA 旅游规划(25 分)
7-10 旅游规划(25 分) 有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条 ...
- Spring MVC运行流程
一.配置阶段 ①web.xml ②DispatcherServlet //Spring MVC总入口 ③配置初始化参数 //classpath:application.xml,用于配置无数个 ...
- Eclipse 一直不停 building workspace完美解决总结
一.产生这个问题的原因多种1.自动升级 2.未正确关闭 3.maven下载lib挂起 等.. 二.解决总结(1).解决方法 方法1.修改eclipse启动文件 eclipse.ini ...
- UMAIL安装教程
UMAIL安装教程 一.UMAIL虚拟机的创建与安装 首先在VM中选择新建虚拟机 选择下一步 选择稍后安装操作系统 选择Linux系统 版本CentOS 7 64位 选择名称与安装位置 选择下一步 选 ...
- git提交项目常用命令及git分支的用法
1.第一步首先从git托管平台clone项目,我这里就使用idea为例: 填写git的url与存放本地目录名及项目名 2.如果你对项目进行了一些修改,就可以执行git命令,进行提交. 有两种方 ...
- Oracle中SQL语句分类
Oracle中SQL语句分类如下:1.DML语句 insert/delete/update/select/merge/explan plan/lock table2.DDL语句 create/atlt ...
- python学习之路前端-CSS
CSS概述 css是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 存在方式有三种:元素内联.页面嵌入和外部引入,比较三种方式的优缺点. 语法:style ...
- PHP $_GET 变量
$_GET 变量 预定义的 $_GET 变量用于收集来自 method="get" 的表单中的值. 从带有 GET 方法的表单发送的信息,对任何人都是可见的(会显示在浏览器的地址栏 ...
- Zookeeper Api
如何使用 Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储,但是 Zookeeper 并不是用来专门存储 ...
- RX系列四 | RxAndroid | 加载图片 | 提交表单
RX系列四 | RxAndroid | 加载图片 | 提交表单 说实话,学RxJava就是为了我们在Android中运用的更加顺手一点,也就是RxAndroid,我们还是先一步步来,学会怎么去用的比较 ...