I am new to Android Job Scheduler, I want to run a task every day morning 9AM without using Alarm manager.
If you are not using AlarmManager, then you won't be able to guarantee that your task will be executed at 9 AM. If its not crucial, you can use WorkManager or JobScheduler, depending on your need.
Note: Using either of these will not guarantee that your job will be executed exactly at 9AM. These will respect doze mode and the job will be executed once during the specified interval which controlled by OS.
Related
Is it possible to use AlarmManager to set periodic works? For instance, could I set an alarm for say every 15min for 24 hours? Or put an alarm that schedules another alarm to awaken the app with some delay? Is there any reason for it to no work as expected?
I've tried using periodic workers with WorkManager but the workers consistently failed to wake up after several hours (I've used 15min periods).
I was reading Guide To Background Tasks, it's well organized and understandable except a few concepts.
Can anyone differentiate between Deferred and Exact background Tasks with some realtime android examples?
Deferred tasks
Every task that is not directly connected to a user interaction and can run at any time in the future can be deferred. The recommended solution for deferred tasks is WorkManager.
WorkManager makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts. See the documentation for WorkManager to learn how to schedule these types of tasks.
Exact tasks
A task that needs to be executed at an exact point in time can use AlarmManager.
To learn more about AlarmManager, see Schedule repeating alarms.
i'm making an app for events and users can set alerts to the events they want. I use the AlarmManager for this, and the problem is on Android 9(Pie), that it seems blocking alarms and the notifications is not showing anymore.
For Android <= 8 there aren't problems.
Any tip/solution here?
Thanks
Alarms do not fire when the device is idle in Doze mode. Any scheduled alarms will be deferred until the device exits Doze. If you need to ensure that your work completes even when the device is idle there are several options available. You can use setAndAllowWhileIdle() or setExactAndAllowWhileIdle() to guarantee that the alarms will execute. Another option is to use the new Schedule tasks with WorkManager, which is built to perform background work either once or periodically. For more information, see Schedule tasks with Schedule tasks with WorkManager.
Ref: Android Developer.
In my Android app I need to do some work every time the user plugs their device. For this purpose right now I use a BroadcastReceiver, which starts my IntentService to do the work when the user plugs the device and stops it when the device becomes unplugged.
Right now I'm thinking of using JobScheduler for Android 5.0+, but what I'm seeing is that with JobScheduler, I would have to schedule my job within the app, by calling
JobScheduler.schedule(JobInfo);
But this is a problem to me, because I want my job to run every time the user connects their device to the charger, even without the user having to open my app.
For this reason, I think one way would be to schedule it the first time the user opens the app, and then always force reschedule, since I cannot trust on the user opening my app every day (which, due to the nature of my app, certainly won't happen).
So, should I stick with BroadcastReceiver or use JobScheduler for Android 5.0+?
And in the case of using JobScheduler, should I schedule my job only once and then always return true in order to force rescheduling?
Thank you.
So, should I stick with BroadcastReceiver or use JobScheduler for Android 5.0+?
Use JobScheduler, this can improve your app’s performance, along with aspects of system health such as battery life. Also, JobScheduler persists through device reboots and supports batch scheduling by which the android system can combine pending jobs thus reducing battery usage. Moreover, you can do distinguish between android versions thus using JobScheduler on Lollipop and up, and AlarmManager on older versions.
And in the case of using JobScheduler, should I schedule my job only once and then always return true in order to force rescheduling?
Now, there are 2 ways to do this :
As you guessed, scheduling your job only once and always returning true in jobFinished() - this should do the trick.
Upon completing a job (originally scheduled by you by calling JobScheduler.schedule(JobInfo)), you schedule another job by calling the same. This will schedule consequent jobs once each job is about to be completed.
Jobscheduler runs in the background and persists through reboots so you should be fine.
I am making an application that makes use of the jobscheduler API.
I want to run a service periodically and when the device is charged. This is the code.
JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPeriodic(3000);
builder.setRequiresCharging(true);
mTestService.scheduleJob(builder.build());
Now when I run this and I unplug the device, the service still runs after 3 secs. There is no effect of setting the setRequiresCharging.
When i comment out builder.setPeriodic(3000), it works perfectly fine. I am not sure as to where I am going wrong.
To identify each job internally, the framework creates a new JobStatus when a new job lands on the scheduler. One of the first things JobStatus does is to check and see if your job has a periodic update interval. If it does, it uses the periodic update interval to determine the latest point in the future that a job must be run, or in other words, if it has a deadline constraint.
One of the criteria for a job to be considered ready to be executed, it that all of its constraints have been satisfied or the deadline of a job has expired. See JobStatus.isReady for more information.
The JobSchedulerService adds several StateControllers used to track when jobs should run and when they must be stopped. One of these controllers is a TimeController, which:
sets an alarm for the next expiring job, and determines whether a
job's minimum delay has been satisfied
To determine if a new alarm should be scheduled, TimeController checks if your job has any time delay or deadline constraints, which all jobs seem to be given if you set a periodic update interval.
I hope that helps you at least understand why your job continues being scheduled despite your battery constraint. But I don't have a solution that can offer a straightforward fix for this at the moment.