0

I'm developing an app which you can schedule your time and It reminds you on time just like google calendar. I use AlarmManager class and set a Repeating task to check Database every one minute and see if there is any alarm on that time or not.

@Override
public void onReceive(Context context, Intent intent) {

    Calendar now = Calendar.getInstance();

    doRepeatingWorks(now.getTimeInMillis()); // Like Checking if one day passed to do some tasks

    checkDbIfThereIsSomeSchedule(now);
}

And I call this to start alarm manager:

    public void setAlarm(Context context) {
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent intent = new Intent(context, AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), G.ALARM_CHECK_INTERVAL_MILIS, pendingIntent);
}

But it's inaccurate and sometimes I figure out that the task killer apps kill my alarm and make it totally worthless.

Although using a foregroundService is battery consuming and it goes on user's nerve with the notification.

Is there any solutions or alternatives for this problem?

Mohammad H
  • 785
  • 2
  • 10
  • 25

2 Answers2

3

I use AlarmManager class and set a Repeating task to check Database every one minute and see if there is any alarm on that time or not

That is a truly awful approach. This sort of behavior is precisely why Doze mode and app standby were added in Android 6.0.

Is there any solutions or alternatives for this problem?"

Schedule an alarm event for first event. When you get control, notify the user about the event, then schedule an alarm event for the next event in sequence. If the user adds a new event that is sooner than your first event, cancel the previous alarm and schedule one for the new first event.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Does it make any changes for task killers problem? Is it more accurate? – Mohammad H Aug 07 '17 at 10:46
  • @Mohammad: On most devices, whatever you consider a "task killer" to has no effect on alarms, regardless of how you use `AlarmManager`. On a few devices, the manufacturer has shipped a "task killer" pre-installed that was poorly written and cancels alarms, perhaps as part of a full "force stop" implementation. There is nothing that you can do about this. – CommonsWare Aug 07 '17 at 10:48
  • Yeah like Asus Tabs. And about your solution, 1)Why it's better , 2) Can I clear all alarms and build list of all available schedule alarms together each time DB changes? – Mohammad H Aug 07 '17 at 10:54
  • @Mohammad: "Why it's better" -- because waking up the device every minute, as your code does, is extremely wasteful. Suppose the user has one event per week. With my approach, you wake up the device once per week. With your approach, you wake up the device 10,080 times. "Can I clear all alarms and build list of all available schedule alarms together each time DB changes?" -- probably, though I would not do it that way. There should be some limit as to how many alarms you can schedule, though such a limit is not documented. – CommonsWare Aug 07 '17 at 10:57
0

You don't need to check there is an alarm in each 1 min. I hope this post helps you - Scheduled Alarm Manager not working Android

Suneesh Ambatt
  • 1,347
  • 1
  • 11
  • 41