我在这里没有理解清楚,希望有人能够帮助我。我正在使用以下方式设置闹钟:
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返回null值。因此,数据未传递给广播接收器。我做错了什么?我该如何传递数据。
非常感谢。