Android Notification通知详解
Android Notification通知详解
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); builder = new Notification.Builder(this); builder.setAutoCancel(true); builder.setSmallIcon(R.drawable.alert); builder.setContentTitle("标题"); builder.setContentText("文本"); builder.setDefaults(Notification.DEFAULT_SOUND| Notification.DEFAULT_VIBRATE);
Intent intent = new Intent(this, SecondActivity.class); PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent,PendingIntent.FLAG_ONE_SHOT); builder.setContentIntent(pIntent); manager.notify(0, builder.build());
- Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel;
- Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效;
- PendingIntent自带Context,而Intent需要在某个Context内运行;
- Intent在原task中运行,PendingIntent在新的task中运行。
- FLAG_ONE_SHOT : 这个PendingIntent只能使用一次。
- FLAG_NO_CREATE : 如果被描述的PendingIntent不存在,那么简单地返回null,而不是创建它。
- FLAG_CANCEL_CURRENT : 如果被描述的PendingIntent已经存在,在即将生成一个新的PendingIntent前,当前的一个要被取消。
- FLAG_UPDATE_CURRENT :如果被描述的PendingIntent已经存在,那么继续保持它,但它其中的数据会因为新Intent而更新。
NotificationCompat.Builder builder = new NotificationCompat.Builder(
MainActivity.this);
builder.setContentText("344444444444");
builder.setContentTitle("33333333333");
builder.setSmallIcon(R.drawable.ic_launcher);
Intent intent = new Intent(MainActivity.this,
ScondActivity.class);
PendingIntent intent2 = PendingIntent.getActivity(
MainActivity.this, 1, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(intent2);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
|
/*** 在状态栏显示通知*/private void showNotification(){// 创建一个NotificationManager的引用 NotificationManager notificationManager = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 定义Notification的各种属性 Notification notification =new Notification(R.drawable.icon, "督导系统", System.currentTimeMillis()); //FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉//FLAG_NO_CLEAR 该通知不能被状态栏的清除按钮给清除掉//FLAG_ONGOING_EVENT 通知放置在正在运行//FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应notification.flags |= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中 notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用 notification.flags |= Notification.FLAG_SHOW_LIGHTS; //DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等//DEFAULT_LIGHTS 使用默认闪光提示//DEFAULT_SOUNDS 使用默认提示声音//DEFAULT_VIBRATE 使用默认手机震动,需加上<uses-permission android:name="android.permission.VIBRATE" />权限notification.defaults = Notification.DEFAULT_LIGHTS; //叠加效果常量//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;notification.ledARGB = Color.BLUE; notification.ledOnMS =5000; //闪光时间,毫秒// 设置通知的事件消息 CharSequence contentTitle ="督导系统标题"; // 通知栏标题 CharSequence contentText ="督导系统内容"; // 通知栏内容 Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 点击该通知后要跳转的Activity PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification传递给NotificationManager notificationManager.notify(0, notification); } ?//删除通知 private void clearNotification(){// 启动后删除之前我们定义的通知 NotificationManager notificationManager = (NotificationManager) this .getSystemService(NOTIFICATION_SERVICE); notificationManager.cancel(0); }} |
1. [代码][Java]代码
Android Notification通知详解根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时,把notification从通知栏里去掉。或者,只要程序在运行就一直显示通知栏图标。下面对Notification类中的一些常量,字段,方法简单介绍一下: 常量: DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等 DEFAULT_LIGHTS 使用默认闪光提示 DEFAULT_SOUNDS 使用默认提示声音 DEFAULT_VIBRATE 使用默认手机震动 【说明】:加入手机震动,一定要在manifest.xml中加入权限: <uses-permission android:name="android.permission.VIBRATE" /> 以上的效果常量可以叠加,即通过 notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE; notification.defaults |= DEFAULT_SOUND (最好在真机上测试,震动效果模拟器上没有)//设置flag位 FLAG_AUTO_CANCEL 该通知能被状态栏的清除按钮给清除掉 FLAG_NO_CLEAR 该通知能被状态栏的清除按钮给清除掉 FLAG_ONGOING_EVENT 通知放置在正在运行 FLAG_INSISTENT 是否一直进行,比如音乐一直播放,知道用户响应常用字段: contentIntent 设置PendingIntent对象,点击时发送该Intent defaults 添加默认效果 flags 设置flag位,例如FLAG_NO_CLEAR等 icon 设置图标 sound 设置声音 tickerText 显示在状态栏中的文字 when 发送此通知的时间戳NotificationManager常用方法介绍: public void cancelAll() 移除所有通知(只是针对当前Context下的Notification) public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification) public void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为id public void notify(int id, Notification notification) 将通知加入状态栏,标记为id
转载于:https://www.cnblogs.com/sharecenter/p/5621013.html
Android Notification通知详解相关推荐
- 四十一、Android Notification通知详解
根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时 ...
- Notification使用详解之三:通过服务更新进度通知在Activity中监听服务进度
为什么80%的码农都做不了架构师?>>> 上次我们讲到如何实现一个可更新的进度通知,实现的方式是启动一个线程模拟一个下载任务,然后根据任务进度向UI线程消息队列发送进度消息,U ...
- Notification(状态栏通知)详解
本节引言: 本节带来的是Android中用于在状态栏显示通知信息的控件:Notification,相信大部分 学Android都对他都很熟悉,而网上很多关于Notification的使用教程都是基于2 ...
- Android SystemUI 架构详解
Android SystemUI 架构详解 本文描述Android系统中一个核心应用SystemUI,详细赘述SystemUI中几大模块功能的实现过程.由于作者水平有限,如发现本文中错误的地方,欢迎指 ...
- JMessage Android 端开发详解
JMessage Android 端开发详解 目前越来越多的应用会需要集成即时通讯功能,这里就为大家详细讲一下如何通过集成 JMessage 来为你的 App 增加即时通讯功能. 首先,一个最基础的 ...
- Android Studio 插件开发详解二:工具类
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...
- Android 动画框架详解,第 1 部分
2019独角兽企业重金招聘Python工程师标准>>> Android 平台提供了一套完整的动画框架,使得开发者可以用它来开发各种动画效果,本文将向读者阐述 Android 的动画框 ...
- Android USB 开发详解
Android USB 开发详解 先附上 Android USB 官方文档 Android通过两种模式支持各种 USB 外设和 Android USB 附件(实现Android附件协议的硬件):USB ...
- Android之canvas详解
首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...
最新文章
- 网友们票选的2018 Best Paper,你pick谁?
- Leap Motion+第六感或引发人机交互革命
- 如何查看keepalived版本号_Linux下Keepalived 安装与配置
- Altium_Designer-PCB的覆铜步骤
- 微软BI 之SSAS 系列 - 在SQL Server 2012 中开发 Analysis Services Multidimensional Project
- 【ARM】在Uboot中运行第一个汇编程序
- Spring Boot注解
- 移动端APP扁平化UI设计解析
- 数据结构与算法之-----栈的应用(一)
- 语言程序推箱子课设报告_学完C语言,可以去哪些应用领域工作?
- 网络安全-Web端安全协议
- Mac运行node.js连接oracle数据库报DPI-1047: Cannot locate a 64-bit Oracle Client library: “dlopen(libclntsh.dy
- 未能加载文件或程序集“XXXX”或它的某一个依赖项。试图加载格式不正确的程序。(已解决)
- 30天自制操作系统(day10)
- K2P在DYNV6自动更新外网IP地址的脚本
- oracle19c创建表空间,Oracle19c 创建表空间
- Apollo MPC OSQP Solver
- 西工大计算机学院保研人数,陕西多所大学保研率超20%,西北工业大学27%,推免999人...
- 「儒系」产品经理:管理预期,做好增长的3个核心要素
- DophinScheduler ui部分 核心代码详细解析——重中之重的src文件夹里究竟有何种玄机
热门文章
- 关于我的文章说明及联系方式
- 在Ubuntu 14.04平台上利用Intel的GPU实现硬件加速--基于VAAPI
- 乐玩自动化测试模块_五大测试框架介绍,附带全套黑马自动化测试视频教程(完结)...
- 【maven】maven jar 包 冲突 的解决方式
- 【kafka】kafka 2.3 版本的kafka topic 分区扩容
- 【shell】shell-grep -v 排除多个输出结果
- 【Flink】flink-1.12 通过 -t 指定模式后无法指定yarn参数
- 60-30-012-使用-Kafka不停机删除topic数据(自动建立topic)md
- 【LogStash】LogStash 配置后无法启动的问题
- javacc解析json报错