0

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?

And_Dev
  • 113
  • 2
  • 12
  • Don't use a file on the SD card for that, use `SharedPreferences`, where you can store data types like booleans or integers. – RogueBaneling Aug 07 '15 at 00:42
  • yes, thats what I plan to use, but I am lost in how to use that value in alarmmanager. Any suggestions? – And_Dev Aug 07 '15 at 00:43
  • Make sure that if you use `getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE)`, you don't also use `getDefaultSharedPreferences`. They need to be the same. Relevant: http://stackoverflow.com/questions/5946135/difference-between-getdefaultsharedpreferences-and-getsharedpreferences – RogueBaneling Aug 07 '15 at 01:22

1 Answers1

0

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)

Community
  • 1
  • 1
RogueBaneling
  • 4,331
  • 4
  • 22
  • 33
  • Thank you for the answer. Yes, I am using broadcast receiver to restart the service that starts my "main activity". Just to double check I updated my logic above. Can you please comment? Also, I am confused about alarm.setRepeating(AlarmManager.RTC_WAKEUP, nextTime, .... please see my change in this part. – And_Dev Aug 07 '15 at 01:12
  • Thank you, I realized my mistake. Let me update my code. – And_Dev Aug 07 '15 at 01:21
  • Sorry to bother you again, can you see my updated question? I am sure that the value from shared preference is correct, but on phone's restart app shows up immediately. Your suggestion will be really helpful – And_Dev Aug 07 '15 at 03:07
  • Can you restate what it is that you're trying to do? The reason that your app is popping up when the phone restarts is because in your `BroadcastReceiver` you are starting `MainActivity.class` which causes the activity's user interface to pop up. – RogueBaneling Aug 07 '15 at 03:18
  • Primarily I have a questionnaire app, that will pop up every 2 weeks. For that, my "broadcast receiver" starts "Main Activity" which does nothing but uses "alarm manager" to start "MyService". "MyService" registers a WiFi receiver for data transfer and also it startsup the "QuestionsActivity". Now the issue is that whenever I restart the phone, "QuestionsActivity" pops up. I double checked the timer value, that seems correct. Any suggestions? – And_Dev Aug 07 '15 at 03:23
  • Your answer makes sense to me, but the issue is that instead of blank "Main Activity" it pops up "QuestionsActivity". Also, I can see the design bug, pls suggest any changes. – And_Dev Aug 07 '15 at 03:27
  • I updated my answer. Don't use the BroadcastReceiver to start an Activity to set the alarm, use the BR to start a service instead. – RogueBaneling Aug 07 '15 at 03:32