用alarmmanager 多次发送PendingIntent
遇到如下问题
service中得一随机数 用alarmmanager 发送PendingIntent的时候,receiver收到的随机数不变。
pendingintent传值经常获取到的值是第一次的值或者null,这个跟第二个参数和最后一个参数选择有关系.
将最后一个参数设置成
PendingIntent.FLAG_UPDATE_CURRENT
即可解决问题。
总结一下pendingIntent的常用FLAG标签:
FLAG_ONE_SHOT:this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
FLAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.
FLAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by >canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
FLAG_UPDATE_CURRENT: if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care >that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
上 面4个flag中最经常使用的是FLAG_UPDATE_CURRENT,因为描述的Intent有更新的时候需要用到这个flag去更新你的描述,否则 组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras。使用FLAG_CANCEL_CURRENT也能做到更新 extras,只不过是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的区别在于能 否新new一个Intent,FLAG_UPDATE_CURRENT能够新new一个Intent,而FLAG_CANCEL_CURRENT则不能, 只能使用第一次的Intent。
另外两flag就比较少用,利用FLAG_ONE_SHOT获取的PendingIntent只能使用一次,再使用PendingIntent也将失败,利用FLAG_NO_CREAT获取的PendingIntent若描述的Intent不存在则返回NULL值.
代码
注意:此代码仅仅实现延时操作,若需要循环发送,需要在receiver将service启动。
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
int random;
// get a random number
random = (int)(Math.random() * 10 + 1);
long triggerTime = SystemClock.elapsedRealtime() + 5 * 1000;
// get pid and tid
int pid = Process.myPid();
long tid = Process.myTid();
Intent i = new Intent(SEND_ACTION);
i.putExtra(RANDOM_NUM, random);
i.putExtra(SERVICE_PID, pid);
i.putExtra(SERVICE_TID, tid);
// get a pending intent which can send braodcast
PendingIntent pi = PendingIntent
.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
// 5seconds later send the pendingintent
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerTime, pi);
return super.onStartCommand(intent, flags, startId);
}
用alarmmanager 多次发送PendingIntent的更多相关文章
- AlarmManager与PendingIntent
1.AlarmManager的作用与PendingIntent的关系 顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是 ...
- Android随笔之——闹钟制作铺垫之AlarmManager详解
说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...
- AlarmManager 实现闹钟的基本功能
先上效果图 这是一个利用AlarmManager做的最简单的闹钟!迟点再把重复响铃(例如星期一,星期三,重复响铃) 1.MainActivity package com.example.domeref ...
- AlarmManager
转自:http://blog.csdn.net/wangxingwu_314/article/details/8060312 1.AlarmManager,顾名思义,就是“提醒”,是Android中常 ...
- app耗电优化之四 使用AlarmManager对任务进行合理安排
AlarmManager 是用来设定定时任务.即用来设定那个任务在什么时候开始执行.为什么和省电有关系?这个需要和AlarmManager的使用先说起.AlarmManager 实际上只起到一个定时发 ...
- 安卓电量优化之AlarmManager使用全部解析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.AlarmManager概述 AlarmManager是安卓系统中一种系统级别的提示服务,可以在我们设定时间或者周期性的执行一个intent,这 ...
- Android 开发 AlarmManager 定时器
介绍 AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为 ...
- AlarmManager的使用和七牛云android SDK上传图片
一学期的课程设计又开始了,虽然以后不搞安卓,但是课设还是想好好完成的,因为之前做过地图开发,所以选了一个跟 这个相关的题目,其实有一个校车系统也可以选,但是之前做过一个相似度接近80%的东西,不想混混 ...
- 定时任务,AlarmManager使用
CoderLt 定时任务,AlarmManager使用 项目需要:实现一个定时提醒的功能 查阅资料知道,需要使用AlarmManager AlarmManager介绍: AlarmManager是 ...
随机推荐
- C++异常处理: try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- asp.net留言板项目源代码下载
HoverTree是一个asp.net开源项目,实现了留言板功能. 前台体验网址:http://hovertree.com/guestbook/ 后台请下载源代码安装. 默认用户名:keleyi 默认 ...
- HighchartsNET快速图表控件-开源
前言: HighchartsNET快速图表控件,基于Highcharts的asp.net web控件.只需几行代码你就能快速生成一个图表. 从此不再担心图表复杂.简单几行代码就可以搞定,节省大量工作时 ...
- 【Win10开发】自定义标题栏
UWP 现在已经可以自定义标题栏了,毕竟看灰色时间长了也会厌烦,开发者们还是希望能够将自己的UI做的更加漂亮,更加与众不同.那么废话不多说,我们开始吧! 首先要了解ApplicationViewTit ...
- Eclipse切换SVN用户
1. 点击windows --> preference --> Team --> SVN,查看当前的SVN接口. 2. 如果SVN接口是JavaHL,那么找到C:\Documents ...
- C#综合揭秘——通过修改注册表建立Windows自定义协议
引言 本文主要介绍注册表的概念与其相关根项的功能,以及浏览器如何通过连接调用自定义协议并与客户端进行数据通信.文中讲及如何通过C#程序.手动修改.安装项目等不同方式对注册表进行修改.其中通过安装项目对 ...
- ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件)
ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件) Startup Class 1.Startup Constructor(构造函数) 2.Configure ...
- js去掉字符串的空格
//去左空格; function ltrim(s){ return s.replace(/(^s*)/g, ""); } //去右空格; function rtrim(s){ re ...
- jQuery静态方法inArray,grep,merge,makeArray方法使用和源码分析
inArray方法 确定第一个参数在数组中的位置,从0开始计数(如果没有找到则返回 -1 ). 示例: var arr = [ 4, "Pete", 8, "John&q ...
- Android刷机教程之LG Nexus 5X线刷官方Nexus系列教程
镜像下载地址:https://developers.google.com/android/nexus/images 1.打开手机 设置-关于手机-点击版本号7次,以打开“开发者选项” 2.返回上一步, ...