遇到如下问题

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的更多相关文章

  1. AlarmManager与PendingIntent

    1.AlarmManager的作用与PendingIntent的关系 顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是 ...

  2. Android随笔之——闹钟制作铺垫之AlarmManager详解

    说实话,之前写的两篇博客Android广播机制Broadcast详解.Android时间.日期相关类和方法以及现在要写的,都算是为之后要写的闹钟应用做铺垫,有兴趣的话,大家可以去看看前两篇博客. 一. ...

  3. AlarmManager 实现闹钟的基本功能

    先上效果图 这是一个利用AlarmManager做的最简单的闹钟!迟点再把重复响铃(例如星期一,星期三,重复响铃) 1.MainActivity package com.example.domeref ...

  4. AlarmManager

    转自:http://blog.csdn.net/wangxingwu_314/article/details/8060312 1.AlarmManager,顾名思义,就是“提醒”,是Android中常 ...

  5. app耗电优化之四 使用AlarmManager对任务进行合理安排

    AlarmManager 是用来设定定时任务.即用来设定那个任务在什么时候开始执行.为什么和省电有关系?这个需要和AlarmManager的使用先说起.AlarmManager 实际上只起到一个定时发 ...

  6. 安卓电量优化之AlarmManager使用全部解析

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 一.AlarmManager概述 AlarmManager是安卓系统中一种系统级别的提示服务,可以在我们设定时间或者周期性的执行一个intent,这 ...

  7. Android 开发 AlarmManager 定时器

    介绍 AlarmManager是Android中常用的一种系统级别的提示服务,在特定的时刻为我们广播一个指定的Intent.简单的说就是我们设定一个时间,然后在该时间到来时,AlarmManager为 ...

  8. AlarmManager的使用和七牛云android SDK上传图片

    一学期的课程设计又开始了,虽然以后不搞安卓,但是课设还是想好好完成的,因为之前做过地图开发,所以选了一个跟 这个相关的题目,其实有一个校车系统也可以选,但是之前做过一个相似度接近80%的东西,不想混混 ...

  9. 定时任务,AlarmManager使用

    CoderLt   定时任务,AlarmManager使用 项目需要:实现一个定时提醒的功能 查阅资料知道,需要使用AlarmManager AlarmManager介绍: AlarmManager是 ...

随机推荐

  1. Gitlab使用总结

    Gitlab日常开发流程 1. 从某一功能分支新建一个自己的开发分支 二. 将master分支clone到本地 mkdir git-test cd git-test\ git clone http:/ ...

  2. seajs的使用--主要了解模块化

    一个使用sea.js的Demo sea.js可以解决命名问题,js文件间的依赖等. index.html内容如下: <!DOCTYPE html> <html xmlns=" ...

  3. SQL 优化总结

    SQL 优化总结 (一)SQL Server 关键的内置表.视图 1. sysobjects         SELECT name as '函数名称',xtype as XType  FROM  s ...

  4. luogg_java学习_10_异常_反射

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! 异常_反射 异常 异常就是导致程序中断执行的一段指令流 ...

  5. IT基础架构规划方案三(IT基础软件和系统规划)

    IT基础软件和系统规划 操作系统选型规划方案 根据对某集团的实际调研,获取了企业业务应用系统的建设情况,随着企业信息化建设的推进,需要对各种信息化管理系统和应用系统的服务器选型进行选型规划,根据不同的 ...

  6. jQuery静态方法parseXML使用和源码分析

    jQuery.parseXML( data ) 接受一个格式良好的 XML 字符串,返回解析后的 XML 文档. 方法 jQuery.parseXML() 使用浏览器原生的 XML 解析函数实现. 在 ...

  7. iOS之weak和strong、懒加载及循环引用

    一.weak和strong 1.理解 刚开始学UI的时候,对于weak和strong的描述看得最多的就是“由ARC引入,weak相当于OC中的assign,但是weak用于修饰对象,但是他们都不会造成 ...

  8. java设计模式 策略模式Strategy

    本章讲述java设计模式中,策略模式相关的知识点. 1.策略模式定义 策略模式,又叫算法簇模式,就是定义了不同的算法族,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户.策略模式属于对象的 ...

  9. Android Studio连接真机没反应?

    刚好遇到这个问题,在网上百度了一下,看到有人分享了引起该问题的几个原因: 手机设置问题.开USB调试 方法: 手机设置 - 开发人员选项 - USB调试 - 勾选 数据线问题. 有的数据线只能用来充电 ...

  10. Eclipse部署项目的原理简介eclipse,wtpwebapps,tomcat

    转载请注明出处! http://www.cnblogs.com/libingbin/ 感谢您的阅读.如果文章对您有用,那么请轻轻点个赞,以资鼓励.