用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是 ...
随机推荐
- 用Javascript模拟微信飞机大战游戏
最近微信的飞机大战非常流行,下载量非常高. 利用JS进行模拟制作了一个简单的飞机大战[此源码有很多地方可以进行重构和优化] [此游戏中没有使用HTML5 任何浏览器都可以运行]. 效果图: 原理:利用 ...
- ORACLE存储过程调用Web Service
1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...
- jquery 之for 循环
jquery 的 for 循环: 1. var userList = [11,22,33,44]; $.each(userList,function(i,item){ console.log(i, i ...
- [注意]SerialPort操作PCI-1621D多串口卡,出现异常"参数不正确"
开发LED大屏显示.40-20mA模拟量输出的时候,经常要与串口打交道.但是Windows自带的SerialPort串口操作组件貌似兼容性 不是太好,或是SerialPort本身有BUG,在操作PCI ...
- HTML5漂亮实用的电子书
效果体验:http://hovertree.com/texiao/html5/2.htm 点击这里下载 支持多种系统Mac,PC,Android,iPhone,iPad和Windows Phone 支 ...
- 关于jquery中用函数来设置css样式
关于jquery中用函数来设置css样式 2016-12-21 23:27:55 1.jquery语法 $(selector).css(name,function(index,value)) name ...
- JavaScript时间处理之几个月前或几个月后的指定日期
在平常项目开发过程中,经常会遇到需要在JavaScript中处理时间的情况,无非两种(1,逻辑处理 2,格式转换处理).当然要说相关技术博,园子里闭着眼睛都能抓一把,但是我要做的是:既然有幸被我碰到 ...
- 独立IP 与 共享IP
最近在阿里云产品体系中折腾的不亦乐乎,在选则服务器的时候,会有独立IP和共享IP的两种不同的选择,这里查阅资料予以记录,独立IP与共享IP的区别: 因为IP资源有限,独立IP就是说一个IP地址只给你一 ...
- 如何使用CSS绘制一个响应式的矩形
背景: 最近因为需要用到绘制类似九宫格的需求,所以研究了一下响应式矩形的实现方案. 有如下几种方案: 使用js来设置元素的高度 使用vw单位 div {width: 50vw; height: 50 ...
- SharePoint 2013 Word 转换PDF服务介绍及示例
前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...