Thanks to google that he has introduced Job Scheduling API in its latest version Android Lollipop. Sometimes we may face the situation where we want to run a particular task later on some specific condition (known as scheduling). We can do this using the JobScheduler APIs provided by Android 5.0 onward now.
In this tutorial, I will explain you how to use the JobScheduler
API to create jobs which will execute in the background only when some specific conditions occur. You can check the API level details here on JobScheduler Android developer’s site. The API is able to batch different jobs at once and run them together with the consideration to save a device’s battery life. This tutorial will show you how to schedule a job running in the background on a button click. The user will be able to cancel it any time on button click.
Follow the steps I will describe below to understand how this all works. Also, please make sure to set your minimum SDK to API 21.
Step1: Create a new android application in your android IDE (assuming you will be using android studio) with minimus SDK version 5.0.
Step2: Write following into main layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/schedule_job" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Schedule Job"/> <Button android:id="@+id/cancel_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel All"/> </LinearLayout>
Step3: Create and write following into SchedulerService.java:
package com.example.jobscheduler; import android.app.job.JobParameters; import android.app.job.JobService; import android.os.Handler; import android.os.Message; import android.widget.Toast; public class SchedulerService extends JobService { private Handler mJobHandler = new Handler( new Handler.Callback() { @Override public boolean handleMessage( Message msg ) { Toast.makeText( getApplicationContext(), "JobService task running", Toast.LENGTH_SHORT ).show(); jobFinished( (JobParameters) msg.obj, false ); return true; } } ); @Override public boolean onStartJob(JobParameters params ) { mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) ); return true; } @Override public boolean onStopJob( JobParameters params ) { mJobHandler.removeMessages( 1 ); return false; } }
Step4: Add below line to your manifest:
<service android:name=".SchedulerService" android:permission="android.permission.BIND_JOB_SERVICE" />
Step5: Finally add these jobs with your main activity. Write below into your main activity file:
package com.example.jobscheduler; import android.app.Activity; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private JobScheduler mJobScheduler; private Button mScheduleJobButton; private Button mCancelAllJobsButton; @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); mJobScheduler = (JobScheduler) getSystemService( Context.JOB_SCHEDULER_SERVICE ); mScheduleJobButton = (Button) findViewById( R.id.schedule_job ); mCancelAllJobsButton = (Button) findViewById( R.id.cancel_all ); mScheduleJobButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { JobInfo.Builder builder = new JobInfo.Builder( 1, new ComponentName( getPackageName(), SchedulerService.class.getName() ) ); builder.setPeriodic( 3000 ); if( mJobScheduler.schedule( builder.build() ) <= 0 ) { //If something goes wrong } } }); mCancelAllJobsButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick( View v ) { mJobScheduler.cancelAll(); } }); } }
OutPut
Extracting Colors In Android 5.0 – “The Palette API”
Clipboard APIs in Android
Android’s Latest Version 5.0 Lollipop – What’s New
Top 10 Android App Development Trends | 2020 Guide
5 Best Resources to Get Started with Android Nougat
Android Studio Introduction
Services – An Android Component
Applying MediaCodec On An Open Source Android Audio Player