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
- C语言第五讲,语句 顺序循环选择.
- C语言第四讲,typedef 关键字,以及作用域
- C语言第三讲,基本数据类型
- 64位内核第二讲,进程保护之对象钩子
- x64内核HOOK技术之拦截进程.拦截线程.拦截模块
- Spring开启方法异步执行
- 64位内核第一讲,和32位内核的区别
- Maven精选系列--发布jar包到Nexus私库
- 《SpringMVC从入门到放肆》五、SpringMVC配置式开发(处理器适配器)
- 《SpringMVC从入门到放肆》四、SpringMVC配置式开发(处理器映射器)
- 什么是Spring Boot?
- 《SpringMVC从入门到放肆》三、DispatcherServlet的url-pattern配置详解
- 《SpringMVC从入门到放肆》二、SpringMVC的执行流程及默认配置
- 《SpringMVC从入门到放肆》一、概述
- java教程
- Java快速入门
- Java 开发环境配置
- Java基本语法
- Java 对象和类
- Java 基本数据类型
- Java 变量类型
- Java 修饰符
- Java 运算符
- Java 循环结构
- Java 分支结构
- Java Number类
- Java Character类
- Java String类
- Java StringBuffer和StringBuilder类
- Java 数组
- Java 日期时间
- Java 正则表达式
- Java 方法
- Java 流(Stream)、文件(File)和IO
- Java 异常处理
- Java 继承
- Java 重写(Override)与重载(Overload)
- Java 多态
- Java 抽象类
- Java 封装
- Java 接口
- Java 包(package)
- Java 数据结构
- Java 集合框架
- Java 泛型
- Java 序列化
- Java 网络编程
- Java 发送邮件
- Java 多线程编程
- Java Applet基础
- Java 文档注释
- fastdfs简单实现
- atop使用介绍
- bugzilla安装详解
- sudo和su的用法
- 网站打开缓慢排查思路
- linux下rsync+inotify实时同步
- mysql迁移数据目录
- Problem with the SSL CA cert (path? access rights?)
- Linux笔记(12)| 几种并发式IO的实现方法
- 使用ansible部署DNS主从(ubuntu)
- “一瓶一证”防伪溯源,腾讯安全领御与百年张裕达成战略合作
- ubuntu离线安装python环境
- ubuntu下解决Unment dependencies问题
- 【Vulnhub】Literally Vulnerable
- 内核通信之 Netlink 源码分析和实例分析