0

Here is the code:

//From on create of normal activty
intent  = new Intent(context, UpdateScoresService.class);
recurringDownloadDaily = PendingIntent.getService(context,0,intent, 0);
recurringDownloadWeekly = PendingIntent.getService(context, 1, intent, 0);

public static class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState){
                final ListPreference background_refresh = (ListPreference) findPreference("background_refresh");
                Preference.OnPreferenceChangeListener refreshListener = new Preference.OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        if(newValue.toString().equals("1")){ /* daily */
                            background_refresh.setSummary("Scores will be refreshed daily");

                            AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                            manager.cancel(recurringDownloadDaily);
                            Log.e("DAILY REFRESH", " ");
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTimeInMillis(System.currentTimeMillis());
                            calendar.set(Calendar.HOUR_OF_DAY,10);
                            calendar.set(Calendar.MINUTE,00);
                            manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, recurringDownloadDaily);
                        }else if(newValue.toString().equals("2")){ /* weekly */
                            Log.e("WEEKLY REFRESH", " ");
                            background_refresh.setSummary("Scores will be refreshed weekly");
                            AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                            manager.cancel(recurringDownloadDaily);
                            manager.cancel(recurringDownloadWeekly);
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTimeInMillis(System.currentTimeMillis());
                            calendar.set(Calendar.HOUR_OF_DAY,10);
                            calendar.set(Calendar.MINUTE,00);
                            manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, recurringDownloadWeekly);
                        }else{ /* manually */
                            background_refresh.setSummary("Scores will be refreshed manually");
                            Log.e("MANUAL REFRESH", " ");
                            AlarmManager manager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
                            manager.cancel(recurringDownloadDaily);
                            manager.cancel(recurringDownloadWeekly);
                        }
                        return true;
                    }
                };
                background_refresh.setOnPreferenceChangeListener(refreshListener);

What seems to be happening is when I select either of the first 2 options, it fires off immediately. I'm not really sure why this is happening, I've looked at examples online and it seems to match what I wrote. I'm trying to schedule either to refresh daily at 10AM or to refresh every 7 days at 10AM.

user2923535
  • 584
  • 1
  • 8
  • 22
  • @MikeM.Hmm, that seems to work for the following day (I'm changing system time on the device) but every day after that it doesn't trigger. – user2923535 Oct 25 '16 at 23:24
  • 1
    Do note that you're setting an inexact alarm. It could be off by a relatively large amount. If you want an exact repeating alarm on KitKat or above, with a `targetSdkVersion`>=19, you need to use a `setExact*()` method instead, setting the alarm again each time it fires. – Mike M. Oct 25 '16 at 23:29
  • Oh wait, I tried it again and it did trigger. Hmm seems to be working now, thanks a lot. @MikeM. Oh one last question, lets says the alarm is supposed to trigger at 10:00 but the device is off. And the device turns on at 10:10. Does the alarm still trigger or is it not going to trigger until the next day? – user2923535 Oct 25 '16 at 23:37
  • Sorry, I didn't see the edit to your comment. Alarms do not persist through a reboot. You have to reset your alarms at boot time, and if you set one with a starting time in the past, as you know now, it'll fire immediately. You'll have to decide how you want to handle that situation. – Mike M. Oct 26 '16 at 00:54

0 Answers0