[英]Call Activity Method from BroadcastReceiver
I am trying to call from my BroadcastReceiver(ReceiverSchedule) to call a method (CancelSchedules) in my Activity(ViewScheduleActivity). 我试图从我的BroadcastReceiver(ReceiverSchedule)调用以在我的Activity(ViewScheduleActivity)中调用一个方法(CancelSchedules)。 The main problem I have is that it the Activity becomes null on the onReceive.
我的主要问题是,它的活动在onReceive上变为null。 I think I'm simply passing it into the intent wrong.
我想我只是将其传递给意图错误。 I have tried the following link: Call an activity method from a BroadcastReceiver class .
我尝试了以下链接: 从BroadcastReceiver类调用活动方法 。
First there is an alarmanager that will get called at the correct time then in the Receiver I want to cancel all alarms. 首先有一个警报器,它将在正确的时间被调用,然后在接收器中我要取消所有警报。 Here is the code in the Activity to set the alarm (this part will work fine).
这是“活动”中用于设置警报的代码(此部分将正常工作)。
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(this, RecieverSchedule.class);
myIntent.setClass(this, recieverSchedule.getClass());
myIntent.putExtra("scheduleJson", gs.toJson(schedule));
myIntent.putExtra("LoginUserAsString", gs.toJson(loginUser));
myIntent.putExtra("PatientAsString", gs.toJson(patientResult));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, i);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(ViewScheduleActivity.this, "Set One Time Alarm at: "+ hour +":" + min, Toast.LENGTH_LONG).show();
Now to pass the Activity over in my BroadCastReceiver I have: 现在要在我的BroadCastReceiver中传递活动,我有:
public void setMainActivityHandler(ViewScheduleActivity main) {
viewScheduleActivity = main;
}
with the declaration on top: 声明放在最前面:
ViewScheduleActivity viewScheduleActivity = null;
Now on create I would like to call the method CancelSchedules which is in my ViewScheduleActivity: 现在创建时,我想在我的ViewScheduleActivity中调用方法CancelSchedules:
public void CancelSchedules()
{
for (int i =0; i< 10; ++i)
{
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Intent myIntent = new Intent(this, RecieverSchedule.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, i);
alarmManager.cancel(pendingIntent);
}
}
In my onReceive in RecieverSchedule I have: 在我的RecieverSchedule中的onReceive中,我有:
public void onReceive(Context context, Intent intent) {
viewScheduleActivity.CancelSchedules();
}
The problem is that viewScheduleActivity is null causing me to be unable to call it. 问题是viewScheduleActivity为null,导致我无法调用它。
I have the following code to pass over the Activity with ReciverSchedule created as a member in the Activity: 我有以下代码来传递活动,并将ReciverSchedule创建为活动的成员:
recieverSchedule = new RecieverSchedule();
recieverSchedule.setMainActivityHandler(this);
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.ANY_ACTION");
registerReceiver(recieverSchedule, callInterceptorIntentFilter);
The setMainActivityHandler will get called and created, but I suspect that I am passing a new RecevierSchedule rather than the set receiverSchdule. setMainActivityHandler将被调用并创建,但是我怀疑我正在传递新的RecevierSchedule而不是设置的receiveSchedule。 If I am correct, how do I pass receiverSchedule into the Alarmmanager?
如果我是正确的,如何将ReceiverSchedule传递给Alarmmanager? I thought registerReceiver would fix this issue but maybe the placement is wrong.
我以为registerReceiver可以解决此问题,但也许位置错误。
Your Activity will likely be long killed and dead by the time the AlarmManager
triggers your BroadcastReceiver
. 到
AlarmManager
触发BroadcastReceiver
时,您的Activity可能会长期被杀死并死亡。 When that happens, Android will create a new process for your application, instantiate your BroadcastReceiver
and call onReceive()
. 发生这种情况时,Android将为您的应用程序创建一个新进程,实例化
BroadcastReceiver
并调用onReceive()
。 This is why the variable viewScheduleActivity
is null
. 这就是为什么变量
viewScheduleActivity
为null
。
As @Athena suggested, you don't need to call back into your Activity in order to cancel alarms. 正如@Athena所建议的那样,您无需回调到Activity即可取消警报。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.