Android Notification 的四种使用方式
实现通知步骤
一般实现通知需要如下步骤:
1.获取 NotificationManager 实例管理通知;
2.实例 Notification 对象;
3.管理事件 Intent;
4.发送通知。
注:如不需在通知出现时,点击时有事件执行,步骤3可以忽略。1. 普通通知
获取 NotificationManager 实例:
NotificationManager 对通知进行管理,调用 Context 的 getSystemService() 方法获取。
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- 1
实例 Notification 对象:
Notification notification= new NotificationCompat.Builder(Context).build();
- 1
此时只创建了一个空的 Notification 对象,没有实际作用,可以在build() 方法之前连缀任意多的方法设置 Notification 对象。现在来设置一些基本设置
Notification notification = new NotificationCompat.Builder(Context)
.setContentText("通知内容")
.setContentTitle("通知标题")
.setSmallIcon(android.R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_round))
.setWhen(System.currentTimeMillis())
.build();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
以上设置了五个方法:
setSmallIcon() 用于设置通知的小图标,只能使用纯 alpha 图层的图片进行设置,小图标会显示在系统状态栏上。(alpha 图层的图片你不知道没关系,UI 会知道的,哈哈,这个我也不知P出来,这里我只是暂时用默认图标代替)
setLargeIcon() 设置通知的大图标,当下拉通知后显示的图标。
setWhen() 指定通知被创建的时间,以毫秒为单位,下拉通知后会将时间显示在相应的通知上。
现在可以发送一个基本通知了
manager.notify(1,notification);
- 1
notify() 方法接收两个参数,参数一 id 指定通知的 id,要保证每个通知的 id 是不同的;参数二 Notification 对象,传入之前创建好的即可。
当次通知出现时,点击是不会有任何效果的,这是因为没有关联 Intent,没有还不简单,给他添加一个就可以了,此时会用到 PendingIntent ,PendingIntent 的获取可以根据需求选择
getActivity(),getBroadcast(),getService() 等静态方法来获取。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.baidu.com"));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(Content)
.setContentIntent(pi)
.build();
- 1
- 2
- 3
- 4
- 5
调用 setContentIntent() 方法,传入 PendingIntent 实例即可,当点击同时会打开浏览器进入百度主页。
我们现在在完善下 Notification
Notification notification = new NotificationCompat.Builder(Context)
...
.setAutoCancel(true)//点击通知头自动取消
.setDefaults(NotificationCompat.DEFAULT_ALL)//设置铃声及震动效果等
.build();
- 1
- 2
- 3
- 4
- 5
控制手机震动等还需要相应的权限
<uses-permission android:name="android.permission.VIBRATE"/>
- 1
也可对铃声,LED灯,震动等分别进行设置
setSound()//铃声
setLights()//LED灯
setVibrate()//震动
- 1
- 2
- 3
- 2.悬挂式通知
//在 build()之前设置 .setFullScreenIntent()
Notification builder = new NotificationCompat.Builder(Context);
Notification notify = builder.setSmallIcon(R.mipmap.ic_launcher_round)
.setPriority(Notification.PRIORITY_DEFAULT) //通知的优先级
.setCategory(Notification.CATEGORY_MESSAGE) //通知的类型
.setContentTitle("通知")
.setAutoCancel(true)
.setContentIntent(pi)
.setContentText("Heads - Up Notification on Android 5.0")
.setFullScreenIntent(pi, true) //不设置此项不会悬挂,false 不会出现悬挂
.build();
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
setPriority() 方法共有5个等级:
1. PRIORITY_MIN - 最低级别(-2);
2. PRIORITY_LOW - 较低级别(-1);
3. PRIORITY_DEFAULT - 默认级别(0);
4. PRIORITY_HIGH - 较高级别(1);
5. PRIORITY_MAX - 最高级别(2);
当发出此类型的通知时,通知会以悬挂的方法显示在屏幕上。
- 3.折叠式通知
折叠式同时需要借助 RemoteViews 来实现
Notification builder = new NotificationCompat.Builder(Context);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.sina.com"));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
// 未下拉的样式 R.layout.collapsed
RemoteViews collapsed = new RemoteViews(getPackageName(), R.layout.collapsed);
collapsed.setTextViewText(R.id.collapsed_text, "关闭状态");
//下拉后的样式R.layout.show
RemoteViews show = new RemoteViews(getPackageName(), R.layout.show);
Notification notify = builder.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher_round)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.setContentText("新浪微博")
.setCustomContentView(collapsed)//下拉前
.setCustomBigContentView(show)//下拉后
.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, notify);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 4.锁屏通知
Android 5.0(API level 21)开始,通知可以显示在锁屏上,通过设置选择是否允许敏感的通知内容显示在安全的锁屏上。
//通过 setVisibility() 方法设置即可
...
.setVisibility(VISIBILITY_PUBLIC)
.build();
- 1
- 2
- 3
- 4
- 5
setVisibility() 方法共有三个选值:
1.VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容;
2.VISIBILITY_PUBLIC : 显示通知的全部内容;
3.VISIBILITY_SECRET : 不显示任何内容,包括图标。
Android Notification 的四种使用方式的更多相关文章
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (一) —— 总览
Android数据的四种存储方式SharedPreferences.SQLite.Content Provider和File (一) —— 总览 作为一个完成的应用程序,数据存储操作是必不可少的. ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (四) —— ContentProvider
ContentProvider是安卓平台中,在不同应用程序之间实现数据共享的一种机制.一个应用程序如果需要让别的程序可以操作自己的数据,即可采用这种机制.并且此种方式忽略了底层的数据存储实现,Cont ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (二) —— SQLite
SQLite是一种转为嵌入式设备设计的轻型数据库,其只有五种数据类型,分别是: NULL: 空值 INTEGER: 整数 REAL: 浮点数 TEXT: 字符串 BLOB: 大数据 在SQLite中, ...
- [Android]Android数据的四种存储方式
存储方式 Android提供以下四种存储方式: SharePreference SQLite File ContentProvider Android系统中数据基本都是私有的,一般存放在“data/d ...
- (转)Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File (三) —— SharePreferences
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息.其存储位置在/data ...
- [转][Android]Android数据的四种存储方式
android.database.sqlite类 SQLiteQueryBuilder java.lang.Object android.database.sqlite.SQLiteQueryBuil ...
- Android数据的四种存储方式
作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别是:SharePreference.SQLite.Content Provider和File. ...
- Android数据的四种存储方式SharedPreferences、SQLite、Content Provider和File
作为一个完成的应用程序,数据存储操作是必不可少的.因此,Android系统一共提供了四种数据存储方式.分别 是:SharePreference.SQLite.Content Provider和File ...
随机推荐
- CodeForces 339B Xenia and Ringroad(水题模拟)
题意:给定 n 个地方,然后再给 m 个任务,每个任务必须在规定的地方完成,并且必须按顺序完成,问你最少时间. 析:没什么可说的,就是模拟,记录当前的位置,然后去找和下一个位置相差多长时间,然后更新当 ...
- SurfaceView实时绘图,视频流
- layui中折叠面板的使用
运用折叠面板后 可以让页面更加整洁 有什么不懂的可以留言 代码放到底部 需要引入的文件 JQuery代码: html代码 <div class="layui-colla-item&qu ...
- 微信公众平台开发框架 Bee.WeiXin
我们来看一下如何通过Bee.WeiXin开发微信公众平台.关于微信公众平台的一般性介绍, 这里不做展开. 园里找一找就可以了. 本文主要是介绍Bee.WeXin, 代码已发布到https://beew ...
- 织梦CMS让channelartlist标签支持currentstyle【转】
打开include\taglib\channelartlist.lib.php找到 $pv->Fields['typeurl'] = GetOneTypeUrlA($typeids[$i]); ...
- Window 服务启动出错 14001
在安装windows服务时,没有异常情况,但是在启动的过程中出现 14001错误. 错误 14001 应用程序无法启动 因为应用程序的并行配置不正确 有关详细信息 请参阅应用程序事件日志 或使用命令行 ...
- C# 异常日志记录
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web; na ...
- C# 读Autofac源码笔记(2)
刚看了下Autofac属性注入的源码 首先看看WithProperty方法 image.png Autofac将我们的属性值,存在了一个list集合中 image.png 然后将这个集合传递到 ...
- Oracle彻底卸载
Oracle彻底卸载 卸载:oracle卸载1.删除注册表:打开注册表:regedit 打开路径: <找注册表 :开始->运行->regedit> HKEY_LOCAL_MAC ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...