Matt Southard
Matt Southard

Reputation: 71

Alarmmanager going off on reboot...but its set for 7 days ahead?

I'm failing to see why this alarm is going off on a reboot...I am setting it 7 days ahead here -

Intent intent = new Intent(MainActivity.this, Reminder.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
            MainActivity.this, 1, intent, 1);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
try {
    am.cancel(pendingIntent);
} catch (Exception e) {
    System.out.println("Derp");
}
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, 7);
long time = calendar.getTimeInMillis();

am.set(AlarmManager.RTC_WAKEUP, time,
            pendingIntent);

Here is my manifest that I have set for alarm to stick around on a reboot - Reminder is the class receiving the alarm-

<receiver android:name="com.practicum.notifications.Reminder" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver> 

Upvotes: 0

Views: 272

Answers (3)

Matt Southard
Matt Southard

Reputation: 71

For future reference, I misunderstood how receving the boot complete action worked. I had the intent filter in both of my receiver classes so they were both running, when instead I needed an intent filter on a new broadcastreceiver class to RESET my alarmmanagers.

Upvotes: 0

Machado
Machado

Reputation: 14489

All alarms are shut off when you power off the Android device.

You need to call setRepeating method

public class AlarmReceiver extends BroadcastReceiver {
  private static final int PERIOD=5000;

  @Override
  public void onReceive(Context ctxt, Intent i) {
    scheduleAlarms(ctxt);
  }

  static void scheduleAlarms(Context ctxt) {
    AlarmManager am = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);

    Intent i = new Intent(ctxt, YourService.class);

    PendingIntent pi = PendingIntent.getService(ctxt, 0, i, 0);

    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
                     SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);
  }
}

Check this answer from CommonsWare.

Upvotes: 0

Jackson Chengalai
Jackson Chengalai

Reputation: 3937

By default, all alarms are canceled when a device shuts down. To prevent this from happening, you can design your application to automatically restart a repeating alarm if the user reboots the device. This ensures that the AlarmManager will continue doing its task without the user needing to manually restart the alarm.

You have to manually reset the alarm once again in Bootup Receiver

 public class SampleBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        // Set the alarm here.
    }
}

Upvotes: 3

Related Questions