Android 通知——Android筑基
通知notification是Android中一个很有特色的功能,当某一个app希望向用户通知一些信息,并且该信息并没有在前台运行,就可以使用通知功能实现。
Android—Notification
- 通知基本用法
- PendingIntent通知意图
- 运行截图
- 完整代码
通知基本用法
不管是在哪里创建的通知,整体步骤大体相同,下面介绍创建通知的具体步骤;
- 首先创建一个
NotificationManager
来对通知进行管理,可以调用Context的getSystemService
方法获取。 getSystemService
方法接受字符串确定获取系统的某个服务,传入**getSystemService(NOTIFICATION_SERVICE);**即可。
实例代码如下:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
- 接下来创建一个
Notification
对象。存储通知所需要的各种信息,利用其有参构造函数实现创建。
Notification有参构造包含三个接受参数:
- 第一个指定通知图标,一般是drawable资源目录。
- 第二个参数指定通知
ticker内容
,通知刚被创建时,会在系统状态栏一闪而过,一种瞬态信息。 - 第三个参数指定通知被创建的时间,毫秒ms为单位。
给出实例代码:
Notification.Builder builder1 = new Notification.Builder(MainActivity.this);
//设置图标
builder1.setSmallIcon(R.drawable.ic_launcher_background);
builder1.setTicker("显示第1个通知");
//设置标题
builder1.setContentTitle("通知");
//消息内容
builder1.setContentText("点击查看详细内容");
//发送时间
builder1.setWhen(System.currentTimeMillis());
创建好通知notification对象之后,需要对通知布局设定,调用Notification
的setLatestEventInfo方法可以给通知设定一个标准布局,该方法接受4个参数:
- 第一个参数Context。
- 第二个参数指定通知标题内容。
- 第三个参数指定通知正文内容。
- 第四个参数暂时填null都行。
实例代码:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
otification.Builder builder1 = new Notification.Builder(MainActivity.this);
//设置图标
builder1.setSmallIcon(R.drawable.ic_launcher_background);
builder1.setTicker("显示第1个通知");
//设置标题
builder1.setContentTitle("通知");//消息内容
builder1.setContentText("点击查看详细内容");//发送时间builder1.setWhen(System.currentTimeMillis());
//设置默认的提示音,振动方式,灯光
builder1.setDefaults(Notification.DEFAULT_ALL);//打开程序后图标消失builder1.setAutoCancel(true);
PendingIntent通知意图
PendingIntent 从名字上看起来就和 Intent 有些类似,它们之间也确实存在着不少共同点。比如它们都可以去指明某一个“意图”,都可以用于启动活动、启动服务以及发送广播等。不同的是,Intent 更加倾向于去立即执行某个动作,而 PendingIntent 更加倾向于在某个合适的时机去执行某个动作。所以, 也可以把 PendingIntent 简单地理解为延迟执行的 Intent。
PendingIntent用法使用一样简单,提供了几个静态方法用于获取PendingIntent的实例,根据需求选择使用getActivity
方法、getBroadcast
方法、或者是getService
方法。
上述几个方法所利用的参数一致:
- 第一个仍然是Context。
- 第二个一般用不到,传入null即可。
- 第三个是Intent意图对象,根据该对象构建PendingIntent意图。
给出实例代码:
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
builder1.setContentIntent(pendingIntent);
Notification notification1 = builder1.build();// 通过通知管理器发送通知
notificationManager.notify(1, notification1);
运行截图
平台:夜神模拟器
完整代码
请见gitee仓库链接:notification。
Android 通知——Android筑基相关推荐
- Android通知,PendingIntent示例
Welcome to Android Notification Example using android PendingIntent. In this tutorial we're going to ...
- 20_Android中apk安装器,通过WebView来load进一个页面,Android通知,程序退出自动杀死进程,通过输入包名的方式杀死进程
场景:实现安装一个apk应用程序的过程.界面如下: 编写如下应用,应用结构如下: <RelativeLayout xmlns:android="http://schemas.an ...
- android activity 被notification启动,Android通知Notification全面剖析
原标题:Android通知Notification全面剖析 通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通 ...
- xamarin android 通知,在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- android每日定时推送通知,Android使用JobScheduler定期推送本地通知实例代码
Android5.0之后提供了JobService和JobScheduler,用于在稍后的某个时间点或者当满足某个特定的条件时执行一个任务.使用JobScheduler,我们可以在用户一段时间没有使 ...
- Android通知频道,通知点
In this tutorial, we'll be looking into how the introduction of Android Oreo has brought a drastic c ...
- android通知悬浮通知_Android通知直接回覆
android通知悬浮通知 Android Notification Direct Reply action lets us reply to the notification message, it ...
- Android动态更改通知图标,修改通知标志 | Android 开发者 | Android Developers
从 8.0(API 级别 26)开始,当关联的应用有活动通知时,启动器图标上会显示通知标志(也称为通知圆点).用户可以长按应用图标以显示通知(以及任何应用快捷方式),如图 1 所示. 默认情况下,这些 ...
- android 通知静音_如何使电话静音(但不包括短信和通知)
android 通知静音 If you don't want to hear your phone ring, but do want to hear text messages and other ...
最新文章
- 基于FPGA的DES加解密系统开发(5000字详细介绍欢迎订阅)
- 分数求和(信息学奥赛一本通-T1209)
- ExtJs radiogroup form.loadRecord方法无法赋值正确解决办法
- 2017年10月9日 冒泡去重复习
- 常用工具软件使用【1】
- 硬盘虚拟分区精灵 v2.3 bt
- mysql workbench6.3.5_mysql workbench
- srt文件的时间轴平移处理
- 高项47个过程及输入、输出、工具解释
- 计算机更改刷新频率,电脑屏幕刷新频率无法更改怎么办
- java高并发系列 - 第1天:必须知道的几个概念
- Android平台App进程优先级
- GAL 高压缩版辅助工具
- MVC之前的那点事儿系列(6):动态注册HttpModule
- 【Matlab】Niblack算法和Otsu算法
- Linux 内存性能检测工具
- excel隐藏的选项卡和命令栏怎么找回?
- 测试hadoop集群的读写与计算能力
- Threejs实现标签,自定义样式显示标签
- 鸿蒙系统图片大全,江南百景图鸿蒙版