Search code examples
androidalarmmanager

setrepeating alarmmanager with changing values


Can we use setrepeating in alarmmanager with changing values? For instance,

alarm.setRepeating(AlarmManager.RTC_WAKEUP, 0,
                    AlarmManager.INTERVAL_DAY*14, pi);

sets the interval to be 14 days. But if my phone restarts the alarm resets from start. I can write the value for the next alarm to file on SD card, but i am confused that how to call that value in alarm manager?

---------------------------------UPDATE 2-----------------------

The updated logic is: In broadcast receiver:

public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, MainActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
    }

In Main Activity:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long nextTime = prefs.getLong("timerval", 0);
Intent ishintent = new Intent(this, MyService.class);
PendingIntent pi = PendingIntent.getService(this, 0, ishintent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, nextTime,
                        AlarmManager.INTERVAL_DAY*14, pi);

While, MyService is a service that start my actual activity, and in that activity, when user clicks "Submit" button, after saving the data, the next timer fire time is calculated and saved like this:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Editor editor = prefs.edit();
Long nextint = System.currentTimeMillis() + 60000;
editor.putLong("timerval", nextint );
editor.commit();

But the issue is that on phone restart app still pop ups. Any ideas?


Solution

  • I am assuming you want to set the alarm to the value stored in SharedPreferences after the phone restarts. In that case you will need to receive the BOOT_COMPLETED intent (see this relevant SO question -- you need to set up a BroadcastReceiver). Then to set your alarm:

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    long nextTime = prefs.getLong("timerval", 0);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, nextTime,
                    AlarmManager.INTERVAL_DAY*14, pi);
    

    Your call to AlarmManager.setRepeating(...) has the arguments wrong. Note the docs. The arguments to setRepeating are (in order): the type, the next time to fire, the interval between alarms [after the first alarm fires], then the pending intent. You are currently setting the time to fire to be 0ms, which I think is not what you want. Instead, you want it to be the time from your SharedPreferences.

    EDIT: It seems that you are using the BroadcastReceiver incorrectly. Right now you are using it to start the MainActivity (which is why it pops up when the phone restarts) but what you need is for it to set the AlarmManager. So create a new Service, and in that service, set the AlarmManager, and in your BroadcastReceiver, instead of calling startActivity() on your MainActivity, call startService() on your new Service. (Remember to end the service after the AlarmManager is set)