1

So far I've been using AlarmManager and BroadcastReceiver like this:

private void setUpStreakResetAlarm() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.add(Calendar.DAY_OF_MONTH, 1);

        Intent intent = new Intent(getApplicationContext(), DailyCounterCheckReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
    }

And in my DailyCounterCheckReceiver class:

@Override
    public void onReceive(Context context, Intent intent) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

        int counterOld = sharedPreferences.getInt(GlobalUtilities.SHARED_PFC_STREAK_COUNTER_OLD_KEY, 0);
        int counterToday = sharedPreferences.getInt(GlobalUtilities.SHARED_PFC_STREAK_COUNTER_KEY, 0);

        SharedPreferences.Editor editor = sharedPreferences.edit();
        // If user has increased counter on this day, increase the old check for tomorrow
        if (counterToday > counterOld) {
            counterOld = counterToday;
            editor.putInt(GlobalUtilities.SHARED_PFC_STREAK_COUNTER_OLD_KEY, counterOld);
            editor.putBoolean("increasedOld", true);
            editor.putBoolean("reset", false);
        }
        // etc.....

But with new Android versions, background tasks like that just get killed and it's very unreliable.

So what can I use instead? Work Manager, Foreground Service, something else? And don't I still need AlarmManager to trigger them?

My use case is extremely simple, so I don't think I need some super complex solution, but there are so many options out there. What is better for my simple case?


Edit:
Angel's comment would solve my reset problem, but I also do the same for triggering notifications at certain times:

@Override
    public void onReceive(Context context, Intent intent) {
        this.context = context;

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong("lastTimeOfNotifTrigger", System.currentTimeMillis());
        editor.apply();

        repository = new NotificationRepository(context);
        repository.startGetNextNotificationAsync(this);
    }

How could I solve that in the easiest way?

Big_Chair
  • 2,781
  • 3
  • 31
  • 58
  • 2
    instead of having background services for resetting prefs at midnight, just add another pref for "Last accessed date". then onCreate(), if this "Last accessed date" is more than 24hours, then reset your other preferences and update the "Last accessed date". – Angel Koh Feb 07 '21 at 12:07
  • @AngelKoh That's a good point, thank you. But I also do the same to trigger reminder notifications in the morning, so I think I need to use some kind of service anyway. I will edit my question. – Big_Chair Feb 07 '21 at 12:13

1 Answers1

0

You could use alarmManager.setExact to schedule the alarms (so that your receiver is called exactly at the time you want even if the phone is sleeping or in Doze mode). Once your receiver is called you can schedule the next. Remember to also add receivers detecting BOOT and PACKAGE_CHANGED so that you can re-schedule the alarms in case the phone is rebooted or the app updated.

You could also use Work Manager as you said to schedule a job, if you don't need it to be run at an exact time.

Your code shouldn't have any other issue running in newer versions of Android if you are not trying to launch an Activity from that receiver (you can't launch an activity anymore from the background).

  • But look at this [this question](https://stackoverflow.com/questions/34729966/alarmmanager-not-working-in-several-devices), starting activity isn't the issue, people seem to be having all kinds of trouble with it. But now testing it on a real with Andorid 10 device for a few days in a row, it actually seems to work. But it didn't work on any of my emulators, so I was getting worried. I suppose that was for other reasons. – Big_Chair Feb 11 '21 at 08:41
  • But then how come so many people are complaining about it? Is AlarmManager wrong to use for this case or not, or why should Work Manager be better? – Big_Chair Feb 11 '21 at 08:43