我有一个应用程序,该应用程序使用AlarmManager设置一个警报,当它启动时它会启动另一个Activity。 AlarmManager接收一个PendingIntent,并在指定的时间到来时产生一个BroadcastReceiver类。我想知道是否有任何方法可以通过进入PendingIntent的Intent对象将参数传递给此BroadcastReceiver?通过Android中的Intent将参数传递给BroadcastReceiver
基本上我想要做的是这样的:
Intent my_intent = new Intent(this, BroadcastService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, my_intent, 0);
my_intent.putExtra("arg1", arg1);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (1000), pendingIntent);
,我希望能够在广播接收器的的onReceive(上下文,意图)方法来检索ARG1。我认为局部变量my_intent是PendingIntent传递给onReceive的第二个参数,但显然这不太正确。是否有可能以这种方式(使用Intent.putExtra())在Activity和BroadcastReceiver之间传递参数,还是应该使用ContentProvider?
谢谢!
伊娃
啊,是的,我错过了;我使用闹钟作为调用特定时间的特定活动的途径,当闹钟响起时,我会创建一个通知,用户随时可以随时将其下拉,然后才能调用该活动,以便我不会不要打扰他们正在做的事情。感谢您的回答! – ivcheto
嗯,添加PendingIntent.FLAG_UPDATE_CURRENT作为标志时得到PendingIntent似乎并没有改变任何东西;我仍然没有从我的原始活动中将意图转交给onReceive – ivcheto
的意图您是否在创建PendingIntent之前调用putExtra()?我不知道PendingIntent何时将Intent序列化,但如果它立即执行,创建PendingIntent后调用putExtra()将不起作用。 – CommonsWare