Android Jobscheduler Tutorial | Job Scheduler

 

Android Jobscheduler :

Android Jobscheduler, Android app’s have wide range of features to give the user a rich and easy to use UI providing all the android in built services to the users.Also for the developers extending their new features put forth in every new release of their software development kit’s Android SDK’s.

 

Android app’s will work in background (android background service) state to listen to broadcast messages, push services and many other features which extends the app functionality.Like wise there comes up with a┬ájob scheduling software i.e., API called Jobscheduler.

 

Android jobscheduler is a much efficient way of dealing with the things.It will provide a wake lock for the app in default way providing the guarantee that device stays awake until the job is finished. Also it provides a better battery efficiency which is a prime point in any app.

 

 

activity_main.xml

As this tutorial deals with Android JobScheduler UI is a simple one. Just two buttons to start and stop the job scheduler.

 

<?xml version="1.0" encoding="utf-8"?>	 	 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"	 	 

 xmlns:app="http://schemas.android.com/apk/res-auto"	 	 

 xmlns:tools="http://schemas.android.com/tools"	 	 

 android:layout_width="match_parent"	 	 

 android:layout_height="match_parent"	 	 

 tools:context=".MainActivity"	 	 

 android:orientation="vertical">	 	 

 <Button	 	 

 android:id="@+id/btn_start"	 	 

 android:layout_width="match_parent"	 	 

 android:layout_height="wrap_content"	 	 

 android:text="Start Service"/>	 	 

 <Button	 	 

 android:id="@+id/btn_stop"	 	 

 android:layout_width="match_parent"	 	 

 android:layout_height="wrap_content"	 	 

 android:text="Stop Service"/>	 	 

</LinearLayout>

 

 

MyJobScheduler.java

Add up a Android Jobscheduler, a jobscheduler starts from this class where we can find the methods which will start and stop the job.

Let us see the methods

 

This method deals with the job start where also we can initialize our MyJobExecutor.java class so that we can execute our task as the job starts.

 

@Override	 	 

public boolean onStartJob(final JobParameters params) {	 	 

 	 	 

 return false;	 	 

}

 

To make it easy to understand i have displayed a small message on the start of the job. Which will get displayed as a Toast message.In onPostExecute method inside onStartJob method as

 

@Override	 	 

public boolean onStartJob(final JobParameters params) {	 	 

 jobExecutor = new MyJobExecutor() {	 	 

 @Override	 	 

 protected void onPostExecute(String str) {	 	 

 Toast.makeText(MyJobScheduler.this, str, Toast.LENGTH_SHORT).show();	 	 

 jobFinished(params,false);	 	 

 }	 	 

 };	 	 

 jobExecutor.execute();	 	 

 return true;	 	 

}

 

And when job is started there should also be a method to stop it

 

@Override	 	 

public boolean onStopJob(JobParameters params) {	 	 

 jobExecutor.cancel(true);	 	 

 return false;	 	 

}

 

Full class code :

Providing the full code for Android Jobscheduler

 

import android.app.job.JobParameters;	 	 

import android.widget.Toast;	 	 

public class MyJobScheduler extends android.app.job.JobService {	 	 

 private MyJobExecutor jobExecutor;	 	 

 @Override	 	 

 public boolean onStartJob(final JobParameters params) {	 	 

 jobExecutor = new MyJobExecutor() {	 	 

 @Override	 	 

 protected void onPostExecute(String str) {	 	 

 Toast.makeText(MyJobScheduler.this, str, Toast.LENGTH_SHORT).show();	 	 

 jobFinished(params,false);	 	 

 }	 	 

 };	 	 

 jobExecutor.execute();	 	 

 return true;	 	 

 }	 	 

 @Override	 	 

 public boolean onStopJob(JobParameters params) {	 	 

 jobExecutor.cancel(true);	 	 

 return false;	 	 

 }	 	 

}


 

If you have any query’s on this tutorial on Android Jobscheduler do let us know in the comment section below.If you like this tutorial do like and share us for more interesting tutorials.

 

Show Buttons
Hide Buttons
Read previous post:
Android swipe to refresh view || Pull down refresh

  Every app needs to fetch data on user interaction that may be using a refresh button or may be...

Close