Android Notification通知详解

Notification:
(一)、简介:
显示在手机状态栏的通知。Notification所代表的是一种具有全局效果的通知,程序一般通过NotificationManager服务来发送Notification。
Android3.0增加了Notification.Builder类,该类可以轻松地创建Notification对象。
(二)、Notification.Builder类中提供的方法:
builder.setAutoCancel(); 设置点击通知后,状态栏自动删除通知。
builder.setSmallIcon(R.drawable.alert); 设置通知小图标
builder.setLargeIcon(R.drawable.alert2); 设置通知大图标
builder.setContentTitle("标题"); 设置通知标题
builder.setContentText("文本");  设置通知内容
builder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE);  设置通知的音乐、振动、LED等。
builder.setSound();  设置通知的音乐
builder.setTick();  设置通知在状态栏的提示文本
builder.setContentIntent();  设置点击通知后将要启动的程序组件对应的PendingIntent

(三)、发送Notification的步骤:(四部曲)
1、调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager服务,它是一个重要的系统服务。应用程序可以通过NotificationManager 向系统发送全局通知;
2、构造Notification.Builder对象;
3、设置Notification.Builder对象的各种属性;
4、通过NotificationManager 的notify()方法发送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());

(五)、PendingIntent:
1、PendingIntent字面意义:等待的,悬而未决的Intent;
2、得到一个 PendingIntent 对象,使用方法类的静态方法 getActivity(Context, int, Intent, int);
3、PendingIntent是一种特殊的Intent。主要的区别在于Intent是立刻执行,而 PendingIntent 的执行不是立刻,而是当条件满足后才发送企图,而且PendingIntent 可以取消;
4、PendingIntent执行的操作实质上是参数传进来的Intent的操作,使用 PendingIntent 的目的在于它所包含的Intent的操作的执行是需要满足某些条件的。
5、主要的使用的地方和例子:通知Notificatio的发送,短消息SmsManager的发送和警报器AlarmManager的执行等。

(六)、Intent和PendingIntent的区别:【掌握,以备面试之需】
  1. Intent是立即使用的,而PendingIntent可以等到事件发生后触发,PendingIntent可以cancel;
  2. Intent在程序结束后即终止,而PendingIntent在程序结束后依然有效;
  3. PendingIntent自带Context,而Intent需要在某个Context内运行;
  4. Intent在原task中运行,PendingIntent在新的task中运行。

(七)、PendingIntent的几个常量:(getActivity(Context, int, Intent, int)方法中的第四个参数)
  1. FLAG_ONE_SHOT  : 这个PendingIntent只能使用一次。
  2. FLAG_NO_CREATE : 如果被描述的PendingIntent不存在,那么简单地返回null,而不是创建它。
  3. FLAG_CANCEL_CURRENT  :  如果被描述的PendingIntent已经存在,在即将生成一个新的PendingIntent前,当前的一个要被取消。
  4. FLAG_UPDATE_CURRENT  :如果被描述的PendingIntent已经存在,那么继续保持它,但它其中的数据会因为新Intent而更新。

设置notification的点击跳转:

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通知详解相关推荐

  1. 四十一、Android Notification通知详解

    根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时 ...

  2. Notification使用详解之三:通过服务更新进度通知在Activity中监听服务进度

    为什么80%的码农都做不了架构师?>>>    上次我们讲到如何实现一个可更新的进度通知,实现的方式是启动一个线程模拟一个下载任务,然后根据任务进度向UI线程消息队列发送进度消息,U ...

  3. Notification(状态栏通知)详解

    本节引言: 本节带来的是Android中用于在状态栏显示通知信息的控件:Notification,相信大部分 学Android都对他都很熟悉,而网上很多关于Notification的使用教程都是基于2 ...

  4. Android SystemUI 架构详解

    Android SystemUI 架构详解 本文描述Android系统中一个核心应用SystemUI,详细赘述SystemUI中几大模块功能的实现过程.由于作者水平有限,如发现本文中错误的地方,欢迎指 ...

  5. JMessage Android 端开发详解

    JMessage Android 端开发详解 目前越来越多的应用会需要集成即时通讯功能,这里就为大家详细讲一下如何通过集成 JMessage 来为你的 App 增加即时通讯功能. 首先,一个最基础的 ...

  6. Android Studio 插件开发详解二:工具类

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...

  7. Android 动画框架详解,第 1 部分

    2019独角兽企业重金招聘Python工程师标准>>> Android 平台提供了一套完整的动画框架,使得开发者可以用它来开发各种动画效果,本文将向读者阐述 Android 的动画框 ...

  8. Android USB 开发详解

    Android USB 开发详解 先附上 Android USB 官方文档 Android通过两种模式支持各种 USB 外设和 Android USB 附件(实现Android附件协议的硬件):USB ...

  9. Android之canvas详解

    首先说一下canvas类: Class Overview The Canvas class holds the "draw" calls. To draw something, y ...

最新文章

  1. 网友们票选的2018 Best Paper,你pick谁?
  2. Leap Motion+第六感或引发人机交互革命
  3. 如何查看keepalived版本号_Linux下Keepalived 安装与配置
  4. Altium_Designer-PCB的覆铜步骤
  5. 微软BI 之SSAS 系列 - 在SQL Server 2012 中开发 Analysis Services Multidimensional Project
  6. 【ARM】在Uboot中运行第一个汇编程序
  7. Spring Boot注解
  8. 移动端APP扁平化UI设计解析
  9. 数据结构与算法之-----栈的应用(一)
  10. 语言程序推箱子课设报告_学完C语言,可以去哪些应用领域工作?
  11. 网络安全-Web端安全协议
  12. Mac运行node.js连接oracle数据库报DPI-1047: Cannot locate a 64-bit Oracle Client library: “dlopen(libclntsh.dy
  13. 未能加载文件或程序集“XXXX”或它的某一个依赖项。试图加载格式不正确的程序。(已解决)
  14. 30天自制操作系统(day10)
  15. K2P在DYNV6自动更新外网IP地址的脚本
  16. oracle19c创建表空间,Oracle19c 创建表空间
  17. Apollo MPC OSQP Solver
  18. 西工大计算机学院保研人数,陕西多所大学保研率超20%,西北工业大学27%,推免999人...
  19. 「儒系」产品经理:管理预期,做好增长的3个核心要素
  20. DophinScheduler ui部分 核心代码详细解析——重中之重的src文件夹里究竟有何种玄机

热门文章

  1. 关于我的文章说明及联系方式
  2. 在Ubuntu 14.04平台上利用Intel的GPU实现硬件加速--基于VAAPI
  3. 乐玩自动化测试模块_五大测试框架介绍,附带全套黑马自动化测试视频教程(完结)...
  4. 【maven】maven jar 包 冲突 的解决方式
  5. 【kafka】kafka 2.3 版本的kafka topic 分区扩容
  6. 【shell】shell-grep -v 排除多个输出结果
  7. 【Flink】flink-1.12 通过 -t 指定模式后无法指定yarn参数
  8. 60-30-012-使用-Kafka不停机删除topic数据(自动建立topic)md
  9. 【LogStash】LogStash 配置后无法启动的问题
  10. javacc解析json报错