https://medium.com/google-developers/scheduling-jobs-like-a-pro-with-jobscheduler-286ef8510129
If you have a repetitive task in your Android app, you need to consider that activities and services can be terminated by the Android system to free up resources. Therefore you can not rely on standard Java schedule like the TimerTasks class.
The Android system currently has two main means to schedule tasks:
Modern Android applications should use the JobScheduler API. Apps can schedule jobs while letting the system optimize based on memory, power, and connectivity conditions
After Android 8.0 there were put limitations on Background running processes , since they drain alott of battery , hence SERVICES DONT RESTART after Android 8 or API 25 ,it it recommmended to use JOBSCHEDULER.
THIS LIMITS DOESNT APPLY FOR FOREGROUND SERVICES & BOUND SERVICES AT SOME EXTENT.
useful video on limitations after API 25 : https://youtu.be/LarihH4b6Z4
useful video : create and execute Job service :
https://youtu.be/N87eQL-JheM
---------------------------------------------------------------------------------------------------
RUNs on main thread.
Use Thread or Asynctask for background thread.
Only works on AI level 21 and above. (though recommended to use only after api 23 , since api 21 and 22 has bugs )
Can run recurring Job at set intervals.
Jobscheduler is available ONLY from android 5 or above API 21
-----------------------------------------------------------------------------------------------
Step 1] create a seperate java service and make it extends to Jobservice class and then implement its 2 main methods onstartjob() and onstopjob()
step2] JOBSCHEDULER needs a special permission to run, hence we give the Permisison in the service Code.
IMP : If you dont give this permission your jobscheduler wont RUN.
android:permission="android.permission.BIND_JOB_SERVICE"
step 3] Now the code runs in the Main thread, so you need to create a thread for background execution.
The OnStartJob() method starts , when the Job service is called.
if the execution of the Job scheduler is terminated in between by the user , ONLY then the OnStopjob() method gets called. It do not get called if the servcie is finished its processes without any termination.
step 4] Create a thread inside the OnstartJob() method and write your code inside that thread.
Step5] The return statement of OnStartJob method and OnstopJob() are very important.
The return type of Onstartjob() says if we have any thread or background code running.
if you HAVE a thread or background process to be executed in the onstartjob() return TRUE, so the process wont be terminated entirely by android .
The return statement of onStopjob() method says if you want the service or process to restart after termination.
If yes pass a True.
Step6] Once your Thread or background process in the onStartjob() method are done , you need to call the method JobFinished(). iF YOU ARE RUNNING ANY CODE ON THE WORKER THREAD inside the onstartjobb(), then use the Jobfinished() inside the thread itself to stop the job.
VERY IMP. jobfinish() takes 2 parameters , Params and a Boolean which decides if you want to reschedule the job.
If you dont use the Jobfinished() then the WAKE LOCK is not released and the CPU keeps on running , draining the battery.
Step7] Now make change to the Activity code and make use of Jobscheduler service.
Create a instance of Jobscheduler service, Make a Componentname object, then create a Jobinfo object and create a new instance of it. Apply its Builder Method on it, to set the configurations for when the job schdular should get started, apply the build() method then.
Later you can schedule the Job and it'll get executed.
IMP : schdeuling the job doesnt mean , it'll get executed at the same time always . Their are alott of bugs with Job scheduler in API 21 and 22 .
use the JOBINFO object to set the configuration and system requirements to be satisfied in order for job to get started.
You can set the required configurations as per your needs.
Some of them explained :
1] setRequirednetworktype() - Type of network you want ()eg - wifi or mobile.
To use wifi network use the Jobinfo.NETWORK_TYPE_UNMETERED parameter.
2] setMinimumLatency() - takes the minimum times under whihc the job should get executed on call.
3] setPersisted() - takes a boolean whihc decides if we want the Job to restart again on reboot.
4] serPeriodic - Tkaes time in milisec , it decides after what interval of time the job should run.
Step 8] Now once the Jobinfo() object is build , use the schedule() of jobscheduler object you created and pass the Jobinfo object to it.
The Schedule() method returns a int value of 1 if sucessfully scheduled the job and 0 on unsucessful scheduling.
The jobscheduler,RESULT_SUCESS , also checks if the job is scheduled or not and similarly returns an INT value.
THE JOBSERVICE WONT RUN UNTIL THE SYSTEM REQUIREMENTS ARE MET.
Step 9] CANCELLING THE JOB.
The are two methods we can use to cancel the JOB in an android application.
1] use the Jobscheduler.cancelAll() method which will cancel all the Jobs running on the Android app.
2] To use the Jobscheduler.cancel(jobId) to cancel a specific JOB.
-------------------------------------------------------------------------------------------------------
IMP NOTES :
1] If the Job is scheduled and the configuration conditions are not met, then the Job will not be executed. But since the Job is scheduled, it will RUN as soon as the Conditions are met.
Example : If a job needs a wifi network to run , then the job will not run if wifi network is off. But if the job is scheduled , THE JOB WILL GET EXECUTED as soon as the wifi network is available (REGARDLESS IF THE APP IS RUNNING OR NOT RUNNING)
2] JOBSCHEDULER IS STILL NOT PERFECT AND HAS BUGS
---------------------------------------------------------------------------------------------------------
mainactivity code (with two user defined methods) :
Comments
Post a Comment