麻烦PendingIntent发送捆绑到一个广播接收器,数据丢失 [英] Trouble sending Bundle with PendingIntent to a Broadcast Receiver, data lost

查看:1141
本文介绍了麻烦PendingIntent发送捆绑到一个广播接收器,数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用AlarmManager和一个BroadcastReceiver类(名为AReceiver.java)添加一些基本的报警功能,我的计划。我的问题是我添加到附着在意图创造PendingIntent被捆绑的数据出现丢失。唯一的捆绑数据,我可以在AReceiver类访问是android.intent.extra.ALARM_COUNT = 1。

I am adding some basic alarm functionality to my program via the use of AlarmManager and a BroadcastReceiver class (named AReceiver.java). My problem is that the data I add to the bundle attached to the Intent creating the PendingIntent appears to be lost. The only bundle data I can access in the AReceiver class is a android.intent.extra.ALARM_COUNT=1.

下面是基本的code的主要活动类创建的意图,PendingIntent和AlarmManager: [code的主要活动 - Notepadv3]

Here is the basic code in the main activity class creating the Intent, PendingIntent and the AlarmManager: [Code in main activity - Notepadv3]

Intent intent = new Intent(Notepadv3.this, AReceiver.class);         
intent.putExtra("teststring","hello, passed string in Extra");               
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);     
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);           
am.set(AlarmManager.RTC_WAKEUP, timeOfNextPeriod.getTimeInMillis(), alarmIntent);

[code在的BroadcastReceiver - AReceiver]

[Code in the BroadcastReceiver - AReceiver]

public void onReceive(Context con, Intent arg1) {
Bundle extrasBundle = arg1.getExtras();
Log.d("broadcast","contains teststring = " + extrasBundle.containsKey("teststring"));
Log.d("broadcast","is empty? = " + extrasBundle.isEmpty());
Log.d("broadcast","to string = " + extrasBundle.toString());
    }   

调试消息说,含有的TestString是假的,是空是虚假的输出整捆的时候,我得到了android.intent.extra.ALARM_COUNT = 1的值。

Debug messages say that contains teststring is FALSE, is empty is FALSE and when outputting the whole bundle, I get the android.intent.extra.ALARM_COUNT=1 value.

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

干杯, 汤姆

推荐答案

您需要更改此行

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, 0);

这个

PendingIntent alarmIntent = PendingIntent.getBroadcast(this, pendingPeriodIntentId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

否则数据丢失

otherwise the data is lost

这篇关于麻烦PendingIntent发送捆绑到一个广播接收器,数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆