PendingIntent、Notification常用方法
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常用方法的更多相关文章
- Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式
前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...
- Android新旧版本Notification
Android新旧版本Notification 在notification.setLatestEventInfo() 过时了 以前: NotificationManager mn = (Notific ...
- Notification
一:普通Notification 1.内容标题setContentTitle(...) 2.大图标setLargeIcon(Bitmap) 3.内容setContentText(...) 4.内容附加 ...
- [置顶] android之Notification版本兼容性问题
首先先来创建一个notification提示 //概要 String tickerText = context.getResources().getText(R.string.app_name).to ...
- 同时显示多个 Notification
主要出在PendingIntent.getActivity();的第二个参数,API文档里虽然说是未被使用的参数(给出的例子也直接写0的),实际上是通过该参数来区别不同的Intent的,如果id相同, ...
- Android学习总结(十五) ———— Notification(状态栏通知)基本用法
一.Notification基本概念 Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...
- Android Notification 版本适配方案
Notification 介绍见:https://developer.android.com/reference/android/app/Notification.html Android api 一 ...
- Androidn Notification的使用,解决找不到setLatestEventInfo方法
转自:http://blog.csdn.net/songyachao/article/details/51245370#comments 今天使用4.0.3使用 Notification notifi ...
- android的几种“通知”方式简单实现(Notification&NotificationManager)
关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种.今天就稍微记录下几种Notification的用法.3.0以 ...
随机推荐
- C++函数重载和函数模板(04)
函数重载 函数重载可以使一个函数名具有多种功能,即具有“多种形态”,这种特性称为多态性. C++的多态性又被直观地称为“一个名字,多个函数”.源代码只指明函数调用,而不说明具体调用哪个函数.编译器的这 ...
- LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)$
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- javascript的一些算法的实用小技巧
一.交换两个数字的值 我们交换两个数字的值想到的方法一般就是用一个新的变变量,让他把一个数存起来,然后在交换两个数字的值,看下面这种. var a = 1, b = 2; //交换两个数字的值 var ...
- python 模块的概念介绍
模块 模块:本质就是一个.py文件分为三部分:内置模块.第三方模块,自定义模块 模块: 顶层文件 python模块python模块可以将代码量较大的程序分割成多个有组织的.彼此独立但又能互相交互的代码 ...
- ios微信支付 v3
V2版本和V3版本存在很大的差异. 1. 从成功通过微信支付商户的资料审核返回的邮件开始: 你可以获得这些参数 appid,appSecret,partnerID, partnerKey(从平 ...
- 最新版multer1.3.0上传文件
完整项目资源下载路径:http://download.csdn.net/detail/qq_28506819/9851744 使用方法: cd到跟目录,然后npm install. 运行项目,npm ...
- 哈尔滨理工大学第六届程序设计团队 I-Team
/* 以前做过一个插队的题,这个类似从后往前操作 */ #include <iostream> #include <stdio.h> #include <algorith ...
- 解决Qt编译动态链接库could not find or load the Qt platform plugin "windows" in.问题
最近用Qt5做了一个项目的界面,在编译成可执行文件EXE之后,运行文件,提示: This application failed to start because it could not find o ...
- 前端的UI设计与交互之色彩篇
一.颜色Red热情.警示 Green成功.通过.安全 Blue专业.科技 Pink典雅.明快.女性 Orange醒目.温暖 Purple高雅.浪漫 Yellow活力.提示 Cyan清新.冷静.结构化 ...
- 引入CSS的方式有哪些?link和@import的有何区别应如何选择【转载】
看到淘宝网页中这样写使用的是import,而很多网站都是使用link,当然还有一些页面比较简单,流量很大的网站,是直接将CSS写在html代码中的?他们有什么区别?CSS用import还是link好? ...