0

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> 
Matt Southard
  • 71
  • 1
  • 7
  • See this: https://developer.android.com/training/scheduling/alarms.html. It's for repeating alarms, but you can use it for non repeating ones as well. – Phantômaxx Jun 18 '15 at 14:04
  • I feel I should comment on this...I mean its going off as in on the reboot, the alarm is executing, not getting shut off – Matt Southard Jun 18 '15 at 14:19

3 Answers3

3

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.
    }
}
Jackson Chengalai
  • 3,907
  • 1
  • 24
  • 39
  • 1
    I feel I should comment on this...I mean its going off as in on the reboot, the alarm is executing upon reboot, not getting shut off. It is set to a notification that seems to happening on the reboot! – Matt Southard Jun 18 '15 at 14:20
0

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.

Community
  • 1
  • 1
Machado
  • 14,105
  • 13
  • 56
  • 97
  • I realize I worded the question poorly...I should have said the alarm is executing right upon reboot, not seven days ahead – Matt Southard Jun 18 '15 at 14:21
0

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.

Matt Southard
  • 71
  • 1
  • 7