【发布时间】:2019-11-11 12:34:05
【问题描述】:
我想启动一个警报并将一个对象传递给将由触发警报的 BroadcastReceiver 子类接收的意图,但无论我传递给 Intent 什么,它都不会被保存并且收到的 Intent 将为空
这是我的代码:
(设置闹钟):
private void startAlarm() {
Girafe girafe = new Girafe("holly");
int hash = 1
// set the date of the alarm to be in one minute
Calendar c = Calendar.getInstance();
c.add(Calendar.MINUTE, 1);
// Create the alarm
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
// I pass here the object I want to be received on alarm fired
intent.putExtra("myObject", girafe);
intent.putExtra("myStr", "hello");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, hash, intent, 0);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}
(接收并发出警报):
public void onReceive(Context context, Intent intent) {
String myStr = intent.getStringExtra("myStr") // I receive something null here
Girage g = (Girafe) intent.getSerializableExtra("myObject"); // same here
}
问题出在哪里?
PS : 我知道这个问题已经在 7 个月前在这里提出了 Intent getting null in onReceive in MyAlarm class even though I sat putExtra while sending intent 但没有人解决它。
【问题讨论】:
-
什么是
hash? -
it will not be save and the Intent received will be null不。意图不为空。但intent.getStringExtra("myStr")将返回 null。如果不是那么清楚。 -
尝试不放长颈鹿。
-
我将 hash 替换为 1(简单请求代码)。事实上,intent.getStringExtra("myStr") 返回 null
-
那么请更改您的主题和我们帖子中的文字。
标签: java android android-intent broadcastreceiver