数据发送到报警管理广播接收机 [英] Send data to the alarm manager broadcast receiver
本文介绍了数据发送到报警管理广播接收机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里失去了一些东西,我希望有人能帮助我。我使用下面设立一个报警:
I am missing something here and I hope someone can help me out. I am setting up an alarm using the following:
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);
和使用以下所述广播接收机
and the broadcast receiver using the following:
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();
}
}
然而意图小号apperently空。我看到的getStringExtra空值。所以数据不被传递到广播接收机。我究竟做错了什么?我怎么能传递数据。
However intent s apperently "empty". I am seeing null value for the getStringExtra. So the data is not being passed to the broadcast receiver. What am I doing wrong? How can I pass data.
感谢你这么多。
推荐答案
您需要将额外添加到意图的之前您将它传递给的PendingIntent:
You need to add the extras to the Intent before you pass it to the 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);
这篇关于数据发送到报警管理广播接收机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文