遇到如下问题

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. Xamarin.Android之封装个简单的网络请求类

    一.前言 回忆到上篇 <Xamarin.Android再体验之简单的登录Demo> 做登录时,用的是GET的请求,还用的是同步, 于是现在将其简单的改写,做了个简单的封装,包含基于Http ...

  2. 自己实现简单的AOP(一)简介

    AOP 和 OOP,在我看来是两种相辅相成的技术,作为OOP的补充,AOP 有着自己特殊的应用场景. 假设,我们需要在Service层实现以下几项基本功能: /// <para>1.自动管 ...

  3. [Excel] WorkBook.SaveAs

    ExcelApplication.WorkBook.SaveAs(Filename, FileFormat, Password, WriteResPassword, ReadOnlyRecommend ...

  4. Asp.net 面向接口可扩展框架之使用“类型转化基础服务”测试四种Mapper(AutoMapper、EmitMapper、NLiteMapper及TinyMapper)

    Asp.net 面向接口可扩展框架的“类型转化基础服务”是我认为除了“核心容器”之外最为重要的组成部分 但是前面博文一出,争议很多,为此我再写一篇类型转化基础服务和各种Mapper结合的例子,顺便对各 ...

  5. [转]DbFirst数据验证

    转自:Data Validate 之 Data Annotation 什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF  Db first中使用 ...

  6. luogg_java学习_09_泛型_集合

    这篇博客总结了半天,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 , 泛型 泛型介绍 1).类内部的属性的类型可以由外部决定: 2) ...

  7. C++_系列自学课程_第_4_课_string_《C++ Primer 第四版》

    相信学习过计算机编程的人,基本应该都接触过C语言,在C语言中处理字符串大家一定多遇到过, 也都知道处理字符串非常麻烦,而在C++里面,由标准库string类提供对可变长的字符串的支持.下面 来看看st ...

  8. 简单工厂模式和策略模式结合使用php

    策略模式是有客户端自行实例化算法类的,而简单工厂模客户端只传参数,不关心对象的生成. 结合两种模式,可以在使用策略模式的时候客户端不再生成算法的对象.修改策略模式的配置类即可. 在之前策略模式基础上, ...

  9. WCF入门教程1——WCF简要介绍

    什么是WCF Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的windo ...

  10. PKCS#1规范阅读笔记2--------公私钥ASN.1结构

    PKCS#1种介绍了哈希算法的OID等的ASN.1结构,因为使用ASN.1的解码工具(比如:ASN1View)时,会自动显示出各OID的含义,所以这里就不说明了.下面就只简单摘抄一下RSA公私钥的AS ...