小编Tom*_*son的帖子

无法将具有PendingIntent的Bundle发送到广播接收器,数据丢失

我通过使用AlarmManager和BroadcastReceiver类(名为AReceiver.java)为我的程序添加了一些基本的报警功能.我的问题是我添加到附加到创建PendingIntent的Intent的包的数据似乎丢失了.我可以在AReceiver类中访问的唯一bundle数据是android.intent.extra.ALARM_COUNT = 1.

以下是创建Intent,PendingIntent和AlarmManager的主要活动类中的基本代码:[主要活动中的代码 - 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);
Run Code Online (Sandbox Code Playgroud)

[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());
    }   
Run Code Online (Sandbox Code Playgroud)

调试消息说包含teststring为FALSE,为空为FALSE,在输出整个包时,我得到android.intent.extra.ALARM_COUNT = 1值.

任何帮助将不胜感激.

干杯,汤姆

android bundle broadcastreceiver

13
推荐指数
1
解决办法
8469
查看次数

标签 统计

android ×1

broadcastreceiver ×1

bundle ×1