Why Using the JobScheduler API? - android

What is JobScheduler ? which type of android apps using this JobScheduler and why ? Please feedback with real example so that I could understand.

Here is a short summary:
1) AlarmManager.- Use it to post a notification or set off an alarm at a very specific time. Use this for executions that do not depend on condition.
2) JobScheduler.- Allows you to have execute jobs based on conditions. This is recommended if your app targets API>21.
3) JobDispatcher.- Similar behavior as JobScheduler, used as a JobScheduler-compatibility layer if your app targets versions lower than API<21. Note that this needs internet for real time execution, any task not executed because of internet not being available will be executed when internet becomes available.

Related

Android recommended and reliable API for periodic background work?

I've been using WorkManager to create notifications for my app. For my purposes I figured PeriodicWorkRequest is the most fitting, but after a bit of testing and reading online it's seems extremely unreliable. Using the minimal interval (15 minutes), and the app being closed, the worker woke up 5-6 times and then seems to be killed.
So how does one go about creating background work that wakes up in reasonable time intervals? What is the best approach for creating event-based notification? My idea was checking for the event (for example, checking for something new in the database) in small time intervals (with 15 minutes also being less than ideal), but seeing as it doesn't work well with PeriodicWorkRequest and is also the recommended approach as per the documentation, what exactly are my options?
Basically, the idea of Android is for you not to be able to do what you want to do because we as developers try to kill the battery.
You need to see how the evolution of the restrictions goes:
Version 6 - Doze:
https://developer.android.com/training/monitoring-device-state/doze-standby
https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-power
Version 7 Another state of Doze with even more restrictions:
https://developer.android.com/about/versions/nougat/android-7.0-changes#perf
Broadcast Restrictions:
https://developer.android.com/guide/components/broadcasts
https://developer.android.com/about/versions/nougat/android-7.0-changes#bg-opt
Version 8.0 Background execution limits:
https://developer.android.com/about/versions/oreo/background#services
Version 9 StandBy Buckets - where depending on how the app is used you have different resources to use - like time to wake up the app, time to use the Network, etc
https://developer.android.com/about/versions/pie/power#buckets
https://developer.android.com/about/versions/12/behavior-changes-all#restrictive-app-standby-bucket
https://developer.android.com/topic/performance/appstandby
Battery Save improvements:
https://developer.android.com/about/versions/pie/power#battery-saver
Power Management Restrictions - really important.
https://developer.android.com/topic/performance/power/power-details
Version 11 and 12 App hibernation
https://developer.android.com/topic/performance/app-hibernation
Long story short - you need to prevent all these restrictions to harm your work. But you need to comply because it is better for the user.
But there is no API that will just say - "f**k all these restrictions and do whatever the dev wants to do."
If you need exact timing - you need AlarmManager.
If you do not know when you need to do your work and depend on the outside - Push Notifications which then can transfer the work to the WorkManager.
If you need periodic work that is not time-critical - you might not use the AlarmMangaer and be sure that the work is finished, but you can't be sure when, because there are many restrictions and the priority will be saving the resources.
Also, you can ask the user to be exempted from Battery Optimization:
https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases
If you want to know why exactly the work is not executed you need to check the JS dump and see what restriction is not satisfied:
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging#use-alb-shell0dumpsys-jobscheduler

How to Fetch data from and display notification in background?

I am working on an android app. In which, I want to automate Notification. The way I am thinking is first,
Fetch data from the server, then
Store in Room DataBase, then
Get data from RoomDb and then Display Notification.
Repeat this every day at least once.
Now, I want this work done in the background under any condition. That is, whether the app in the background or whether the app is closed or whether the phone is restart.
So in any situation, Fetch data→Store it→Display Notification.
I found many android background processing libraries. Such as AlarmManager, JobScheduler, BroadcastReceiver, JobIntentService, Firebase Job Dispatcher, WorkManager, etc. I am working on API 19 to API 28 or higher. These libraries have limitations and having challenges in background processing.
So, which library will be suitable for me to build an Automate Notification which works well on API 19 to API 28 or higher.
You can use Jetpack WorkManager. It is backwards compatible up to API 14.
WorkManager internally uses JobScheduler on devices with API 23+
and a combination of BroadcastReceiver + AlarmManager on devices with API 14-22.
Ensures task execution, even if the app or device restarts.

Long network operation on Oreo

In my Android application the user has an option of using the application in offline mode. When the user opts to enter the offline mode, I download all the content from the server(which might take even upto 7 minutes) for offline usage. The usage of the application henceforth is dependent on the download of offline content.
I am using a service to download the offline content. But the service may not work in Android 8 if the app goes to the background. So what is the best approach to download the offline content for Android 8? Is it a foreground service or JobIntentService or a WorkManager?
Anything that is backed by JobScheduler — which includes JobIntentService and WorkManager — has a 10-minute limit. You indicate that your work may take up to 7 minutes, which makes me somewhat nervous.
In the short term, make your existing service be a foreground service, as that will keep your code working (other than any problems that Doze mode might impose).
If your 7-minute download work is really a series of smaller things that add up to 7 minutes, you might eventually migrate to WorkManager. Divide your work into smaller chunks and set up chained work with WorkManager, so you are certain to not go over the 10-minute limit for any of those chunks of work. Plus, WorkManager lets you establish constraints to say that your work should only be performed if you have an Internet connection. Right now (late August 2018), though, WorkManager is only 1.0.0-alpha07, so I would not ship a product based on WorkManager until it at least reaches a 1.0.0 final version.
The best approach would be to use WorkManager. As stated in the docs that:
WorkManager is intended for tasks that require a guarantee that the
system will run them even if the app exits, like uploading app data to
a server, or downloading data from server.
The benefits of using WorkManager over services includes handling of doze, standby, battery optimizations and constraint execution etc.
You can schedule a worker with WorkManager to download data for your app from server, and once data is available, you can go on with your offline mode.

Is WorkerManager a realiable way to implement alarm/reminder feature in the app?

We notice that AlarmManagerCompat alone, isn't a reliable way to implement alarm/ reminder feature in our app, due to different AlarmManager behaviour in different version of OS. (For instance, Doze mode)
Initially, we plan to use Evernote's android-job library, to help us implement alarm/ reminder feature in our app.
However, along the way, we also notice that Google just release WorkerManager.
So far, WorkerManager works well for us, when we run some one-time background jobs (Almost immediate, with internet connectivity constraint) after the app quit.
We have plan to use WorkerManager to implement alarm/ reminder feature.
I was wondering, how reliable is WorkerManager to implement such feature? Has anyone try it out? We are targeting API 15 and above.
WorkManager is not appropriate for anything that must fire at a specific time as jobs, including those used by WorkManager or android-job, will not fire while the device is dozing.
For exact timing, you should absolutely be using AlarmManagerCompat and specifically, setExactAndAllowWhileIdle() which fires an alarm at exactly the specified time on all API levels.
As your exact timed alarm can and will happen while the device is dozing, your app should not require network connectivity to post your alarm/reminder notification. Ideally, the information should be in the PendingIntent itself and not even need any database fetch/etc.

Tracking users periodically in doze mode

I want to track users in specific period and times of days. I start alarm manager in every 2 minutes (i get periodic time from server and this is dynamic in my application) and try to get location about one minute, after that i stopped getting location and i save location taken on DB. anyway, this approach run very well but in api level 23 and above that, my application for doze and standby mode cannot run well and not call alarms in specific time! I used setExactAndAllowWhileIdle method for api level 23 and above that but not worked well.
The documents said, i can't use more alarms in doze and standby mode.
My question is, how can track user in android 6 and above?
While working with alarm in doze, use setAlarmClock() instead of set() as it is specifically designed for AlarmClocks, so it can run even in doze as well.
Alternatively,
Have you ever heard of Firebase-job-dispatcher ?
Firebase JobDispatcher is an open-source library that provides an API
similar to JobScheduler in the Android platform. Firebase
JobDispatcher serves as a JobScheduler-compatibility layer for apps
targeting versions of Android lower than 5.0 (API level 21).
Firebase JobDispatcher supports the use of Google Play services as an
implementation for dispatching (running) jobs, but the library also
allows you to define and use other implementations: For example, you
might decide to use JobScheduler or write your own, custom code.
Because of this versatility, we recommend that you use this Firebase
JobDispatcher if your app targets a version of Android lower than 5.0
(API level 21).
You should go for it. They have got pretty good configuration techniques as well.

Categories

Resources