PendingIntent

PendingIntent它的直译是:待处理意图,这样翻译,大家就猜出它的作用是什么了,用于处理一些定义但是不立即使用的意图,最常见的就是用户点击通知,然后跳转指定的页面:

最常用的方法估计就下面这个了:

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags)

类似的还有:

  • PendingIntent.getActivity(context, 0, notifyIntent, 0);
  • PendingIntent.getService(context, 0, notifyIntent, 0);
  • PendingIntent.getBroadcast(context, 0, notifyIntent, 0);

第一个参数是上下文,第二个参数 requestCode,第三个参数是 Intent,第四个参数是对参数的操作标识

第一、三个没什么好说的,重点介绍另外两个:

第二个参数:requestCode:

Private request code for the sender
当同时有多个通知的时候,可通过这个参数来互相区别

第四个参数:flags

FLAG_CANCEL_CURRENT:
Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.
如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。

FLAG_UPDATE_CURRENT:
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.
如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras。

FLAG_NO_CREATE:
Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it.
如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null。

FLAG_ONE_SHOT:
Flag indicating that this PendingIntent can be used only once.
该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。

FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的区别在于能否新new一个Intent,FLAG_CANCEL_CURRENT能够新new一个Intent,而FLAG_UPDATE_CURRENT则不能。

Notification

显示一则通知,旧的Notification有不少方法是已经过时的,下面是新的:

          public void create(Context context){
Notification notification = new Notification.Builder(context)
//设置通知标题
.setContentTitle("标题")
//设置通知栏显示内容
.setContentText("测试内容")
//设置通知栏点击时的意图,就比如上面的PendingIntent
.setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))
//通知首次出现在通知栏,带上升动画效果的
.setTicker("测试通知")
//通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
.setWhen(System.currentTimeMillis())
//设置该通知优先级
.setPriority(Notification.PRIORITY_DEFAULT)
//设置这个标志当用户单击面板就可以让通知将自动取消
.setAutoCancel(true)
//ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,
//用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,
//主动网络连接)
.setOngoing(false)
//向通知添加声音、闪灯和振动效果的最简单、
//最一致的方式是使用当前的用户默认设置
.setDefaults(Notification.DEFAULT_VIBRATE)
//设置通知小ICON
.setSmallIcon(R.mipmap.ic_launcher)
.build();

PendingIntent、Notification常用方法的更多相关文章

  1. Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式

    前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...

  2. Android新旧版本Notification

    Android新旧版本Notification 在notification.setLatestEventInfo() 过时了 以前: NotificationManager mn = (Notific ...

  3. Notification

    一:普通Notification 1.内容标题setContentTitle(...) 2.大图标setLargeIcon(Bitmap) 3.内容setContentText(...) 4.内容附加 ...

  4. [置顶] android之Notification版本兼容性问题

    首先先来创建一个notification提示 //概要 String tickerText = context.getResources().getText(R.string.app_name).to ...

  5. 同时显示多个 Notification

    主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...

  6. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

  7. Android Notification 版本适配方案

    Notification 介绍见:https://developer.android.com/reference/android/app/Notification.html Android api 一 ...

  8. Androidn Notification的使用,解决找不到setLatestEventInfo方法

    转自:http://blog.csdn.net/songyachao/article/details/51245370#comments 今天使用4.0.3使用 Notification notifi ...

  9. android的几种“通知”方式简单实现(Notification&NotificationManager)

    关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种.今天就稍微记录下几种Notification的用法.3.0以 ...

随机推荐

  1. python基础条件和循环

    一.if语句 1.if后表达式返回值为true则执行其子代码块,然后此if 语句到此终结,否则进入下一分支判断,直到满足其中一个分支,执行后终结if 2.expression可以引入运算符:not,a ...

  2. JDBC基本开发

    JDBC基本开发步骤 一:注册驱动 方式一:DriverManager.registerDriver(new Driver()); //存在注册两次问题,性能较低,消耗资源 方式二:Class.for ...

  3. 用 eclipse 创建一个简单的 meaven spring springMvc mybatis 项目

    下面是整体步骤: 1: 先创建一个Maven 项目: 选择跳过骨架: 因为要搭建的是 web 项目  所以这个地方选择 war 包; 点击完成 这样就完成 Maven项目的搭建: 接下俩 先把 Mav ...

  4. 关于python如果没有numpy模块如何处理

    1.在python中,你在python的shell输入>>>import numpy 但是编译器告诉你没有numpy库,这时候你就要导入python库,那么如何导入呢 2.收下访问h ...

  5. daterangepicker 使用方法以及各种小bug修复

    双日历时间段选择插件 — daterangepicker是bootstrap框架后期的一个时间控件,可以设定多个时间段选项,也可以自定义时间段,由用户自己选择起始时间和终止时间,时间段的最大跨度可以在 ...

  6. time函数获取时间与本地时间不一致

    修改php.ini,将“date.timezone”项修改为“date.timezone = PRC”. 大陆内地可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/U ...

  7. Python Linear algebra

    Linear algebra 1.模块文档 NAME numpy.linalg DESCRIPTION Core Linear Algebra Tools ---------------------- ...

  8. Long Long Message(后缀数组)

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 30427   Accepted: 12 ...

  9. 学习笔记TF060:图像语音结合,看图说话

    斯坦福大学人工智能实验室李飞飞教授,实现人工智能3要素:语法(syntax).语义(semantics).推理(inference).语言.视觉.通过语法(语言语法解析.视觉三维结构解析)和语义(语言 ...

  10. 【Aladdin Unity3D Shader编程】之三 光照模型(二)

    高光反射模型 Specular=直射光*pow(cosθ,高光的参数) θ:是反射光和视野方向的夹角 编写高光反射Shader Shader "AladdinShader/07 Specul ...