WakefullIntentService register two alarms and possibly cancel one

87 views Asked by At

I am using the WakefullIntentService by CommonsWare and would like to register two alarms. I have read How to set two different WakefulIntentService AlarmListeners? but do have some questions about it:

First of all, I notice that the answer above has force set to true in the WakefulIntentService.scheduleAlarms. I set both my alarms in my MainActivity, and possibly also in my settings panel in my SettingsActivity using force = false. Now I took a look at the code and see that if I do not use force = true, the second alarm will not be set. If you set force to true here, do you possibly schedule multiple instances of the same Listener?

The second question is that my second alarm as four intervals (set with setInexactRepeating) which users can adjust in the setting screen. There is the daily, weekly, monthly and never option. Can the alarm handle this "change" when a user changes this option in the settings panel? Would it register a second listener instead of changing the current one?

And third, as you see above, the last option is to never let the alarm trigger, as in I would like to cancel the alarm. Currently when you call WakefulIntentService.cancelAlarms(getActivity()); all alarms are cancelled (or am I wrong here?). How do you make sure the first alarm remains active, while the second is cancelled ?

Currently I am using one BackgroundService which extends WakefulIntentService. By adding actions to intents I execute specific functions. Both my listeners implement WakefulIntentService.AlarmListener.

A snippet from my main activity:

SharedPreferences pref = getSharedPreferences("digest", 0);

if(pref.getInt("mode", 1) != 3) {
    WakefulIntentService.scheduleAlarms(new DigestListener(), this, false);
}

// Start the service if enabled
if (getSharedPreferences("settings", 0).getBoolean("service", true)) {
    WakefulIntentService.scheduleAlarms(new DailyListener(), this, false);
}

and a two snippets from my settings panel fragment:

if (isChecked) {
    WakefulIntentService.scheduleAlarms(new DailyListener(), getActivity(), false);
} else {
    WakefulIntentService.cancelAlarms(getActivity());
}
getActivity().getSharedPreferences("settings", 0)
        .edit()
        .putBoolean("service", isChecked)
        .apply();


final SharedPreferences pref = getActivity().getSharedPreferences("digest", 0);
digestSpinner.setSelection(pref.getInt("mode", 1));
digestSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
        pref.edit().putInt("mode", position).apply();
        Log.e("SettingsFragment", position + "");
        if (position != 3) {
            WakefulIntentService.scheduleAlarms(new DigestListener(), getActivity(), false);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parentView) {}

});
1

There are 1 answers

0
CommonsWare On BEST ANSWER

scheduleAlarms() does not support what you are looking for. Please maintain your own AlarmManager alarms, and either use WakefulIntentService following the basic usage instructions, or use Android's own WakefulBroadcastReceiver.