将数据发送到警报pipe理器广播接收器

我在这里错过了一些东西,希望有人能帮助我。 我正在使用以下设置闹钟:

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的空值。 所以数据没有被传送到广播接收机。 我究竟做错了什么? 我怎样才能传递数据

非常感谢

Solutions Collecting From Web of "将数据发送到警报pipe理器广播接收器"

在将其传递给PendingIntent 之前,您需要将附加内容添加到Intent中:

 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);