Search code examples
javaandroidbroadcastreceiveralarm

Recover alarm counter after reboot


In my application, I need to send notifications when is passed like 1 week, or 1 month after the user has done some action in the app... It's working with a BroadcastReceiver like that >>

public void startMyReceiver() {
    Long time = new GregorianCalendar().getTimeInMillis() + 1000 * 60 * 60 * 24 * 30; <-- for 1 month (it's getting numeric overflow, but is working)
    Long interval = 2592000000L;

    Intent intentAlarm = new Intent(this, MyReceiver.class);

    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, interval, PendingIntent.getBroadcast(this, 2, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}

Then the simple receiver class >>

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent){
        sendNotification();
    {
}

Then in AndroidManifest.xml >>

<application>
<receiver android:name="mypackage.MyReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
</application>

I've set that "android.intent.action.BOOT_COMPLETED" thinking that it was going to recover the counter and keep counting till the time ends...

But it's starting my receiver instantly when the device is BOOTED UP...

How can I set the receiver for 1 month and after REBOOT it keep counting??

-- EDIT -- NEW QUESTION --

How would be the easiest way to store the time and do the calculation? Something like this?? >>

1 - When defining the alarm first time, I save the current time, in millisseconds in Long firstTime and define the alarm to firstTime+longOneMonth;

2 - In the Receiver for boot, I get the current time again and save it in Long rebootTime, then I do:

Long timeDifference = rebootTime - firstTime;
Long differenceToMonth = longOneMonth - timeDifference;

And set the Receiver again to time rebootTime + differenceToMonth??

-- EDIT 2 --

Maybe if I convert the first receiver setting millisseconds to a date string and apply() it to shared preferences, when rebooting, I could convert that to millisseconds again... I guess it would work and be easier


Solution

    1. Define another BroadcastReceiver for boot.
    2. Store the time (One month later) somewhere
    3. All your alarms would gone when device boot up. So when the device boot up, call another function which would set an alarm again. But pay attention you should consider some time have been passed since last time you set an alarm ( for example 7 days ago you set the alarm ) so you should always calculate the difference between the set time and now time;

    EDIT : Easiest way to calculate time is that when you are setting alarm, you store right now time as millisecond. Next time you get now time again and minus it with previous time you stored. that is the time difference!

    At the end minus the one month time with difference value

    Long time = 
    new GregorianCalendar().getTimeInMillis() + 
    ( 1000 * 60 * 60 * 24 * 30 ) -
    TIME_DIFFERENCE