Alarm Receiver 使用 Intent无法更新值的解决方法

 

Alarm Receiver 使用 Intent无法更新值的解决方法


在新建PendingIntent时,指定Flag: FLAG_UPDATE_CURRENT

如:
PendingIntent sender=PendingIntent.getBroadcast(
                 myActivity.this,0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

I am using marks' method of alarmreceiver and appservice. I am trying
to pass a record id to the alarmintent through putextra. and receive
it in ontimealarmreceiver in getExtra.

What I observe is, first time record id goes correct (say 11) second
time record id is sent 12, but in receiver i receive record id = 11.
third time too record id -11


whats wrong ?


regards

 


 
  

   

   
 

您需要先登录才能发帖。
要发帖,您需要先加入此论坛。
请先在订阅设置页上更新您的昵称,然后再进行发帖。
您没有发帖的权限。
    
  
Mark Murphy    查看个人资料   翻译成中文(简体) 翻译内容(查看原文内容) 
  更多选项 2009年12月17日, 下午7时52分 

发件人:"Mark Murphy" <mmur...@commonsware.com>
日期:Thu, 17 Dec 2009 06:52:12 -0500 (EST)
当地时间:2009年12月17日(星期四) 下午7时52分
主题:Re: [android-developers] wrong intent getting supplied to OneTimeAlarmReceiver.
打印 | 单个帖子 | 显示原始帖 | 举报此帖 | 查找此作者的帖子

> I am using marks' method of alarmreceiver and appservice. I am trying
> to pass a record id to the alarmintent through putextra. and receive
> it in ontimealarmreceiver in getExtra.

> What I observe is, first time record id goes correct (say 11) second
> time record id is sent 12, but in receiver i receive record id = 11.
> third time too record id -11


> whats wrong ?

 

PendingIntent recycles Intents, in effect. Try specifying
FLAG_UPDATE_CURRENT when you create your PendingIntent.


--
Mark Murphy (a Commons Guy)
http://commonsware.com
Android App Developer Books: http://commonsware.com/books.html

 

http://groups.google.com/group/android-developers/browse_thread/thread/d3507afeda5f33ba#

Android中,批量设置多个alarm并处理它们到达时的逻辑,可以通过使用`AlarmManager`服务结合`BroadcastReceiver`和`PendingIntent`来实现。下面是具体的步骤: 1. 创建`Intent`和`PendingIntent`:为每个需要定时触发的任务创建一个`Intent`,并由这个`Intent`生成对应的`PendingIntent`。这些`PendingIntent`将被用于在指定时间触发一个广播。 2. 使用`AlarmManager`设置alarm:通过`AlarmManager`的`set()`方法或者`setRepeating()`方法来设置一次性或者重复的alarm。`setRepeating()`方法适用于那些需要周期性执行的任务。对于批量设置,可以循环调用这个方法。 3. 创建`BroadcastReceiver`来处理alarm:编写一个`BroadcastReceiver`类,重写`onReceive()`方法,在这里编写当alarm触发时需要执行的逻辑。 4. 在AndroidManifest.xml中注册`BroadcastReceiver`:确保你的`BroadcastReceiver`在应用的Manifest文件中被声明,这样系统才能识别并响应通过`AlarmManager`设置的alarm。 5. 接收并处理alarm:当设置的alarm到达时,系统会发送一个包含之前创建的`PendingIntent`的广播,`BroadcastReceiver`的`onReceive()`方法会被调用,你可以在这里执行具体的逻辑处理。 6. 注意电源管理:如果需要保证在设备休眠时也能准时触发alarm,可以使用`setExact()`和`setExactAndAllowWhileIdle()`方法来设置精确的alarm。 下面是一个简化的代码示例: ```java // 创建Intent Intent intent = new Intent(context, MyAlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // 获取AlarmManager实例 AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // 设置alarm Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 10); // 设置10秒后触发 alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); // MyAlarmReceiver.java public class MyAlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // 在这里处理alarm逻辑 } } ``` 在AndroidManifest.xml中注册`BroadcastReceiver`: ```xml <receiver android:name=".MyAlarmReceiver" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值