-2

I'm trying to make an app with some scheduled tasks and an accessibility service logging info behind it.

The problem I see right now is that my tasks, which I use WorkManager to run the Workers at the time of the event, sometimes they don't run at the exact time I scheduled them to. I understand that this happens when the phone is using resources so Android pushes my task back to when it's free to do it, but how would I do it so that it always runs at the time I tell it to?

I've looked into it and found AlarmManager with the setExactAndAllowWhileIdle property could be of use, but I read in the docs that there's a limit to how many times this can run. I've also looked into Quartz-Scheduler, but I have no idea whether this will work in these cases.

The accessibility service has stopped working when not using my app for a long time too, and I imagine is the same problem (though not completely sure).

Would disabling "Battery Optimisation" for my app help with all of this? Or what would be a good approach to it?

Stasky
  • 107
  • 1
  • 5

1 Answers1

0
  1. How "exact" you need it to be? If it must be really exact maybe you should consider push notifications. Just have in mind that if the notifications are not with a high priority your work will be handled again by the WorkManager. But with high priority, the user must interact with the notification otherwise the notification will be stoped. It will mean that you are using push where you need to just use the WorkManager.
  2. Have in mind that with any vendor it will be different. There is no exact answer.
  3. WorkManager is not something that gives less than the other options. In the end, you have some restrictions coming from Android. If you are using JobScheduler and AlarmManager you need to handle them on your own.
  4. WorkManager handles the work by using JobScheduler and AlarmManager underneath, but the cool thing is that it has its own DB so you won't be losing the works on app restart. So basically it is more and not less.
  5. From my own experience I know that on some Chinese device(forgot the name) - the WorkManager and the JobScheduler behaved the same, but with Battery optimization off - the WorkManager behaved better. Long story short - the more you do the better. You will handle more vendors.
  6. And yes - you should check the power buckets info. If you are in a lower power bucket and it depends on how much the user interacts with your app - the less you will be executed. Power optimization - off - will help you. https://developer.android.com/topic/performance/power/power-details
  7. Try checking the JobScheduler dump(As your work is actually a Job underneath). It will tell you what you are missing for your work to run. Maybe you have been executed too many times - you will see QUOTA not satisfied. And if you are making Network calls you have an amount of time per 24h. If you have used it you will see CONNECTIVITY not satisfied. use: adb shell dumpsys jobscheduler More on it here: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging
  8. I assume you are familiar with the flex interval: https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#flexible_run_intervals

Also there is a site with a summary of how different vendors work:

https://dontkillmyapp.com/

Yavor Mitev
  • 1,363
  • 1
  • 10
  • 22
  • 1. As exact as possible and shouldn't require any interaction by the user. 2. Thought about that, didn't know if there was something that would help me across the board. 3-4-5. So I imagine Workers with Battery Optimization off would be my best bet for this. 6-7-8. Didn't know of any of this! I'll check it out and try mess around with it. Thanks very much! – Stasky Jun 28 '21 at 15:11