我在这里遗漏了一些东西,希望有人可以帮助我。我正在使用以下设置警报:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent broadcast_intent = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, broadcast_intent, 0);
broadcast_intent.putExtra("test", "ValueReceived"); //data to pass
Date date = someVariable.getDateTime();
long triggerAtTime = date.getTime();
alarmManager.set(AlarmManager.RTC_WAKEUP, triggerAtTime, pendingIntent);
和使用以下内容的广播接收器:
public class AlarmBroadcastReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm has been received "+intent.getStringExtra("test"), Toast.LENGTH_LONG).show();
}
}
然而,意图显然是“空洞的”。我看到getStringExtra的空值。因此数据不会传递给广播接收器。我究竟做错了什么?我如何传递数据。
非常感谢
答案 0 :(得分:20)
您需要将额外内容添加到意图 之前将其传递给PendingIntent:
Intent broadcast_intent = new Intent(this, AlarmBroadcastReceiver.class);
broadcast_intent.putExtra("test", "ValueReceived"); //data to pass
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, broadcast_intent, 0);