Search code examples
androidandroid-activitybroadcastreceiveralarmmanagerandroid-pendingintent

Call Activity Method from BroadcastReceiver


I am trying to call from my BroadcastReceiver(ReceiverSchedule) to call a method (CancelSchedules) in my Activity(ViewScheduleActivity). The main problem I have is that it the Activity becomes null on the onReceive. 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.

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:

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:

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:

public void onReceive(Context context, Intent intent) {
    viewScheduleActivity.CancelSchedules();
}

The problem is that viewScheduleActivity is null causing me to be unable to call it.

I have the following code to pass over the Activity with ReciverSchedule created as a member in the Activity:

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. If I am correct, how do I pass receiverSchedule into the Alarmmanager? I thought registerReceiver would fix this issue but maybe the placement is wrong.


Solution

  • Your Activity will likely be long killed and dead by the time the AlarmManager triggers your BroadcastReceiver. When that happens, Android will create a new process for your application, instantiate your BroadcastReceiver and call onReceive(). This is why the variable viewScheduleActivity is null.

    As @Athena suggested, you don't need to call back into your Activity in order to cancel alarms.