【发布时间】:2010-09-16 17:09:54
【问题描述】:
我正在通过使用 AlarmManager 和 BroadcastReceiver 类(名为 AReceiver.java)向我的程序添加一些基本的警报功能。我的问题是我添加到附加到创建 PendingIntent 的 Intent 的捆绑包中的数据似乎丢失了。我可以在 AReceiver 类中访问的唯一捆绑数据是 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);
[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 为 FALSE,为空为 FALSE,当输出整个包时,我得到 android.intent.extra.ALARM_COUNT=1 值。
任何帮助将不胜感激。
干杯, 汤姆
【问题讨论】:
标签: android bundle broadcastreceiver