SlideShare a Scribd company logo
1 of 14
ANDROID
SCHEDULING
BY
12210 WARDA ASHRAF RANA
13017 SHIZA ATIQUE
13240 GHOUS BUX 13240
CONTENT
1- Definition
2- Job Scheduler
3- Working
4- Module Boundary
5- Package Format
6- Example.
DEFINITION:
First we have to know what is scheduling, “ The process of systematically arranging
numerical content in horizontal or vertical columns is called scheduling”.
Android scheduling is a module that contains logic that detects whether or not the device is
in an idle state, and may be rebooted without user disruption.
The Android system currently has two main means to schedule tasks:
the (outdated) Alarm Manager
the JobScheduler API.
ALARM MANAGER
Android Alarm Manager allows you to access system alarm.
By the help of Android Alarm Manager in android, you can schedule your application to
run at a specific time in the future.
It works whether your phone is running or not.
The Android Alarm Manager holds a CPU wake lock that provides guarantee not to
sleep the phone until broadcast is handle
but it is harder for noobs so google make things easy for us. now we talk about job
scheduler
Modern Android applications should use the JobScheduler API. Apps can schedule jobs
while letting the system optimize based on memory, power, and connectivity conditions
JOBSCHEDULER
The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the
JobScheduler class. This API allows to batch jobs when the device has more resources
available. In general this API can be used to schedule everything that is not time critical for
the user.
Compared to a custom SyncAdapter or the alarm manager, the JobScheduler supports
batch scheduling of jobs. The Android system can combine jobs so that battery consumption
is reduced. JobManager makes handling uploads easier as it handles automatically the
unreliability of the network. It also survives application restarts. Here are example when
you would use this job scheduler:
USES
Tasks that should be done once the device is connect to a power supply
Tasks that require network access or a Wi-Fi connection.
Task that are not critical or user facing
Tasks that should be running on a regular basis as batch where the timing is not critical
WORKING
A unit of work is encapsulated by a JobInfo object. This object specifies the scheduling criteria.
The job scheduler allows to consider the state of the device, e.g., if it is idle or if network is
available at the moment. Use the JobInfo.Builder class to configure how the scheduled task
should run. You can schedule the task to run under specific conditions, such as:
Device is charging
Device is connected to an unmetered network
Device is idle
Start before a certain deadline
CONT(WORKING)
Start within a predefined time window, e.g., within the next hour
Start after a minimal delay, e.g., wait a minimum of 10 minutes
These constraints can be combined. For example, you can schedule a job every 20 minutes,
whenever the device is connected to an unmetered network. Deadline is a hard constraint,
if that expires the job is always scheduled.
To implement a Job, extend the JobService class and implement the onStartJob and
onStopJob. If the job fails for some reason, return true from on the onStopJob to restart
the job. The onStartJob is performed in the main thread, if you start asynchronous
processing in this method, return true otherwise false. Here is the example of Job
Scheduler :
EXAMPLE
class MyJobScheduler : JobService(){
override fun onStartJob(p0: JobParameters?): Boolean {
return false
}
override fun onStopJob(p0: JobParameters?): Boolean {
return false
}
}
CONT(EXAMPLE)
OnStartJob :
onStartJob will be called when the job execution gets started. It takes JobParameters as a parameter.
You can the job info like job id by using that parameter. The return type for this job is Boolean that
means you have to inform the system whether you want to start the job again after finishing their job or
not. If you want to cancel the job after finishing the execution then simply return false which means the
system will call onStopJob method and automatically cancel the specified job.
OnStopJob :
onStop Job will be called by the system. When the service is running and some of the criteria are not
satisfying the service then the android system will automatically call onStop Job. Let suppose you have
registered a job scheduler that required a network connection and your service is running. Then you
disable your net connection. Then the android system will call the onStopJob. It is also returning a
Boolean value that means you want to register the job again or you want to let it finish.
CONT
If you want your service to be prevented from booting or Restarting the android device so you
have to add boot complete permission and set your job setPersisted(true) in your JobInfo.
android:permission="android.permission.RECEIVE_BOOT_COMPLETED“
Now the last part comes into the picture execution of the service.
CONT
class MainActivity : AppCompatActivity() {
val JOB_ID = 1001;
val REFRESH_INTERVAL : Long = 15 * 60 * 1000 // 15 minutes
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
scheduleJob()
}
private fun scheduleJob() {
val jobScheduler = applicationContext
.getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
val componentName = ComponentName(this, MyJobScheduler::class.java)
val jobInfo = JobInfo.Builder(JOB_ID, componentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.setPeriodic(REFRESH_INTERVAL)
.build()
jobScheduler.schedule(jobInfo)
}
}
CONT
First of all, we have to take system services for “JOB_SCHEDULER_SERVICE”.
Then we have to create our component name object that takes two parameters
first is context and the second is your Service class.
The last and main part is JobInfo you have to build your JobInfo object by using
JobInfo.Builder class. It will contain all your information regarding the job like
the job id all the criteria that are required for your service.
I suggest you read all the criteria that JobInfo provides to be a master in
scheduling the jobs.
Here we have done. Hope this blog will help you to understand the Job Scheduler
in Android.
THANK YOU

More Related Content

Similar to Android scheduling.pptx

Andrew - Job scheduler
Andrew - Job schedulerAndrew - Job scheduler
Andrew - Job schedulerrendra toro
 
Analysing in depth work manager
Analysing in depth work managerAnalysing in depth work manager
Analysing in depth work managerbhatnagar.gaurav83
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8Utkarsh Mankad
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAnuradha Weeraman
 
Android 5.0 internals and inferiority complex droidcon.de 2015
Android 5.0 internals and inferiority complex droidcon.de 2015Android 5.0 internals and inferiority complex droidcon.de 2015
Android 5.0 internals and inferiority complex droidcon.de 2015Aleksander Piotrowski
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1Utkarsh Mankad
 
Mobile Fest 2018. Yonatan Levin. WTF with Android Background Restrictions
Mobile Fest 2018. Yonatan Levin. WTF with Android Background RestrictionsMobile Fest 2018. Yonatan Levin. WTF with Android Background Restrictions
Mobile Fest 2018. Yonatan Levin. WTF with Android Background RestrictionsMobileFest2018
 
What is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A WalkthroughWhat is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A WalkthroughWednesday Solutions
 
MarGotAspect - An AspectC++ code generator for the mARGOt framework
MarGotAspect - An AspectC++ code generator for the mARGOt frameworkMarGotAspect - An AspectC++ code generator for the mARGOt framework
MarGotAspect - An AspectC++ code generator for the mARGOt frameworkLeonardo Arcari
 
People code events 1
People code events 1People code events 1
People code events 1Samarth Arora
 
Angular resolver tutorial
Angular resolver tutorialAngular resolver tutorial
Angular resolver tutorialKaty Slemon
 
Not Quite As Painful Threading
Not Quite As Painful ThreadingNot Quite As Painful Threading
Not Quite As Painful ThreadingCommonsWare
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)Khaled Anaqwa
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 ThreadingDiego Grancini
 
eFront V3.7 Extensions Architecture
eFront V3.7 Extensions ArchitectureeFront V3.7 Extensions Architecture
eFront V3.7 Extensions Architecturepapagel
 
Part 1: ng-grid and a Simple REST API
Part 1: ng-grid and a Simple REST APIPart 1: ng-grid and a Simple REST API
Part 1: ng-grid and a Simple REST APIreneechemel
 
ng-grid and a Simple REST API
ng-grid and a Simple REST APIng-grid and a Simple REST API
ng-grid and a Simple REST APIBackand Cohen
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowKaren Morton
 

Similar to Android scheduling.pptx (20)

Andrew - Job scheduler
Andrew - Job schedulerAndrew - Job scheduler
Andrew - Job scheduler
 
Analysing in depth work manager
Analysing in depth work managerAnalysing in depth work manager
Analysing in depth work manager
 
Threads handlers and async task, widgets - day8
Threads   handlers and async task, widgets - day8Threads   handlers and async task, widgets - day8
Threads handlers and async task, widgets - day8
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the Trenches
 
Android 5.0 internals and inferiority complex droidcon.de 2015
Android 5.0 internals and inferiority complex droidcon.de 2015Android 5.0 internals and inferiority complex droidcon.de 2015
Android 5.0 internals and inferiority complex droidcon.de 2015
 
Android service, aidl - day 1
Android service, aidl - day 1Android service, aidl - day 1
Android service, aidl - day 1
 
Mobile Fest 2018. Yonatan Levin. WTF with Android Background Restrictions
Mobile Fest 2018. Yonatan Levin. WTF with Android Background RestrictionsMobile Fest 2018. Yonatan Levin. WTF with Android Background Restrictions
Mobile Fest 2018. Yonatan Levin. WTF with Android Background Restrictions
 
What is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A WalkthroughWhat is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A Walkthrough
 
MarGotAspect - An AspectC++ code generator for the mARGOt framework
MarGotAspect - An AspectC++ code generator for the mARGOt frameworkMarGotAspect - An AspectC++ code generator for the mARGOt framework
MarGotAspect - An AspectC++ code generator for the mARGOt framework
 
People code events 1
People code events 1People code events 1
People code events 1
 
Angular resolver tutorial
Angular resolver tutorialAngular resolver tutorial
Angular resolver tutorial
 
Not Quite As Painful Threading
Not Quite As Painful ThreadingNot Quite As Painful Threading
Not Quite As Painful Threading
 
Concurrency and parallel in .net
Concurrency and parallel in .netConcurrency and parallel in .net
Concurrency and parallel in .net
 
Advanced android app development
Advanced android app developmentAdvanced android app development
Advanced android app development
 
Android Training (Services)
Android Training (Services)Android Training (Services)
Android Training (Services)
 
Android App Development - 07 Threading
Android App Development - 07 ThreadingAndroid App Development - 07 Threading
Android App Development - 07 Threading
 
eFront V3.7 Extensions Architecture
eFront V3.7 Extensions ArchitectureeFront V3.7 Extensions Architecture
eFront V3.7 Extensions Architecture
 
Part 1: ng-grid and a Simple REST API
Part 1: ng-grid and a Simple REST APIPart 1: ng-grid and a Simple REST API
Part 1: ng-grid and a Simple REST API
 
ng-grid and a Simple REST API
ng-grid and a Simple REST APIng-grid and a Simple REST API
ng-grid and a Simple REST API
 
Performance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, HowPerformance Instrumentation for PL/SQL: When, Why, How
Performance Instrumentation for PL/SQL: When, Why, How
 

Recently uploaded

一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样AS
 
Down bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirtsrahman018755
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理AS
 
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样Fi
 
一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书F
 
Beyond Inbound: Unlocking the Secrets of API Egress Traffic Management
Beyond Inbound: Unlocking the Secrets of API Egress Traffic ManagementBeyond Inbound: Unlocking the Secrets of API Egress Traffic Management
Beyond Inbound: Unlocking the Secrets of API Egress Traffic Managementseank14
 
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书AS
 
Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...
Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...
Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...APNIC
 
Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...
Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...
Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...ZurliaSoop
 
Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Dewi Agency
 
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理AS
 
The Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdfThe Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdfe-Market Hub
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理A
 
原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样
原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样
原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样AS
 
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样AS
 
Dan Quinn Commanders Feather Dad Hat Hoodie
Dan Quinn Commanders Feather Dad Hat HoodieDan Quinn Commanders Feather Dad Hat Hoodie
Dan Quinn Commanders Feather Dad Hat Hoodierahman018755
 
Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303Dewi Agency
 
Washington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers ShirtWashington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers Shirtrahman018755
 
@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)
@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)
@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)Obat Cytotec
 
一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理F
 

Recently uploaded (20)

一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
一比一原版(毕业证书)新西兰怀特克利夫艺术设计学院毕业证原件一模一样
 
Down bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirtsDown bad crying at the gym t shirts
Down bad crying at the gym t shirtsDown bad crying at the gym t shirts
 
一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理一比一原版英国格林多大学毕业证如何办理
一比一原版英国格林多大学毕业证如何办理
 
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
一比一原版(UWE毕业证书)西英格兰大学毕业证原件一模一样
 
一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书一比一原版贝德福特大学毕业证学位证书
一比一原版贝德福特大学毕业证学位证书
 
Beyond Inbound: Unlocking the Secrets of API Egress Traffic Management
Beyond Inbound: Unlocking the Secrets of API Egress Traffic ManagementBeyond Inbound: Unlocking the Secrets of API Egress Traffic Management
Beyond Inbound: Unlocking the Secrets of API Egress Traffic Management
 
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
一比一定制(Waikato毕业证书)新西兰怀卡托大学毕业证学位证书
 
Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...
Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...
Registry Data Accuracy Improvements, presented by Chimi Dorji at SANOG 41 / I...
 
Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...
Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...
Jual obat aborsi Bekasi ( 085657271886 ) Cytote pil telat bulan penggugur kan...
 
Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303Loker Pemandu Lagu LC Semarang 085746015303
Loker Pemandu Lagu LC Semarang 085746015303
 
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
一比一原版(Dundee毕业证书)英国爱丁堡龙比亚大学毕业证如何办理
 
The Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdfThe Rise of Subscription-Based Digital Services.pdf
The Rise of Subscription-Based Digital Services.pdf
 
一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理一比一原版布兰迪斯大学毕业证如何办理
一比一原版布兰迪斯大学毕业证如何办理
 
原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样
原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样
原版定制(Glasgow毕业证书)英国格拉斯哥大学毕业证原件一模一样
 
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
一比一原版(毕业证书)新加坡南洋理工学院毕业证原件一模一样
 
Dan Quinn Commanders Feather Dad Hat Hoodie
Dan Quinn Commanders Feather Dad Hat HoodieDan Quinn Commanders Feather Dad Hat Hoodie
Dan Quinn Commanders Feather Dad Hat Hoodie
 
Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303Lowongan Kerja LC Yogyakarta Terbaru 085746015303
Lowongan Kerja LC Yogyakarta Terbaru 085746015303
 
Washington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers ShirtWashington Football Commanders Redskins Feathers Shirt
Washington Football Commanders Redskins Feathers Shirt
 
@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)
@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)
@OBAT ABORSI 3 BULAN@ OBAT PENGGUGUR KANDUNGAN 3 BULAN (087776558899)
 
一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理一比一原版帝国理工学院毕业证如何办理
一比一原版帝国理工学院毕业证如何办理
 

Android scheduling.pptx

  • 1. ANDROID SCHEDULING BY 12210 WARDA ASHRAF RANA 13017 SHIZA ATIQUE 13240 GHOUS BUX 13240
  • 2. CONTENT 1- Definition 2- Job Scheduler 3- Working 4- Module Boundary 5- Package Format 6- Example.
  • 3. DEFINITION: First we have to know what is scheduling, “ The process of systematically arranging numerical content in horizontal or vertical columns is called scheduling”. Android scheduling is a module that contains logic that detects whether or not the device is in an idle state, and may be rebooted without user disruption. The Android system currently has two main means to schedule tasks: the (outdated) Alarm Manager the JobScheduler API.
  • 4. ALARM MANAGER Android Alarm Manager allows you to access system alarm. By the help of Android Alarm Manager in android, you can schedule your application to run at a specific time in the future. It works whether your phone is running or not. The Android Alarm Manager holds a CPU wake lock that provides guarantee not to sleep the phone until broadcast is handle but it is harder for noobs so google make things easy for us. now we talk about job scheduler Modern Android applications should use the JobScheduler API. Apps can schedule jobs while letting the system optimize based on memory, power, and connectivity conditions
  • 5. JOBSCHEDULER The Android 5.0 Lollipop (API 21) release introduces a job scheduler API via the JobScheduler class. This API allows to batch jobs when the device has more resources available. In general this API can be used to schedule everything that is not time critical for the user. Compared to a custom SyncAdapter or the alarm manager, the JobScheduler supports batch scheduling of jobs. The Android system can combine jobs so that battery consumption is reduced. JobManager makes handling uploads easier as it handles automatically the unreliability of the network. It also survives application restarts. Here are example when you would use this job scheduler:
  • 6. USES Tasks that should be done once the device is connect to a power supply Tasks that require network access or a Wi-Fi connection. Task that are not critical or user facing Tasks that should be running on a regular basis as batch where the timing is not critical
  • 7. WORKING A unit of work is encapsulated by a JobInfo object. This object specifies the scheduling criteria. The job scheduler allows to consider the state of the device, e.g., if it is idle or if network is available at the moment. Use the JobInfo.Builder class to configure how the scheduled task should run. You can schedule the task to run under specific conditions, such as: Device is charging Device is connected to an unmetered network Device is idle Start before a certain deadline
  • 8. CONT(WORKING) Start within a predefined time window, e.g., within the next hour Start after a minimal delay, e.g., wait a minimum of 10 minutes These constraints can be combined. For example, you can schedule a job every 20 minutes, whenever the device is connected to an unmetered network. Deadline is a hard constraint, if that expires the job is always scheduled. To implement a Job, extend the JobService class and implement the onStartJob and onStopJob. If the job fails for some reason, return true from on the onStopJob to restart the job. The onStartJob is performed in the main thread, if you start asynchronous processing in this method, return true otherwise false. Here is the example of Job Scheduler :
  • 9. EXAMPLE class MyJobScheduler : JobService(){ override fun onStartJob(p0: JobParameters?): Boolean { return false } override fun onStopJob(p0: JobParameters?): Boolean { return false } }
  • 10. CONT(EXAMPLE) OnStartJob : onStartJob will be called when the job execution gets started. It takes JobParameters as a parameter. You can the job info like job id by using that parameter. The return type for this job is Boolean that means you have to inform the system whether you want to start the job again after finishing their job or not. If you want to cancel the job after finishing the execution then simply return false which means the system will call onStopJob method and automatically cancel the specified job. OnStopJob : onStop Job will be called by the system. When the service is running and some of the criteria are not satisfying the service then the android system will automatically call onStop Job. Let suppose you have registered a job scheduler that required a network connection and your service is running. Then you disable your net connection. Then the android system will call the onStopJob. It is also returning a Boolean value that means you want to register the job again or you want to let it finish.
  • 11. CONT If you want your service to be prevented from booting or Restarting the android device so you have to add boot complete permission and set your job setPersisted(true) in your JobInfo. android:permission="android.permission.RECEIVE_BOOT_COMPLETED“ Now the last part comes into the picture execution of the service.
  • 12. CONT class MainActivity : AppCompatActivity() { val JOB_ID = 1001; val REFRESH_INTERVAL : Long = 15 * 60 * 1000 // 15 minutes override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) scheduleJob() } private fun scheduleJob() { val jobScheduler = applicationContext .getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler val componentName = ComponentName(this, MyJobScheduler::class.java) val jobInfo = JobInfo.Builder(JOB_ID, componentName) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) .setPeriodic(REFRESH_INTERVAL) .build() jobScheduler.schedule(jobInfo) } }
  • 13. CONT First of all, we have to take system services for “JOB_SCHEDULER_SERVICE”. Then we have to create our component name object that takes two parameters first is context and the second is your Service class. The last and main part is JobInfo you have to build your JobInfo object by using JobInfo.Builder class. It will contain all your information regarding the job like the job id all the criteria that are required for your service. I suggest you read all the criteria that JobInfo provides to be a master in scheduling the jobs. Here we have done. Hope this blog will help you to understand the Job Scheduler in Android.