1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user3454910
  • 89
  • 1
  • 10
  • 1
    CancelSchedules doesn't need to be in an activity. Put it in a separate helper class and make it static. problem solved. – Athena Mar 01 '15 at 05:08
  • Your `PendingIntent.getBroadcast()` call is wrong. You are passing the variable `i` as the `flags` parameter. This will not do what you want. I'm guessing that you want to use that as the `requestCode` parameter. – David Wasser Mar 03 '15 at 18:25

1 Answers1

0

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274