Android apps will work in the background (Android Background Service) mode to listen to broadcast messages, push services, and many other features that extend the functionality of the app. Similarly, it comes with job scheduling.
The Android Job Scheduler is a very efficient way of dealing with things. It will provide a wake lock for the app by default, which will provide a guarantee that the device will stay awake until the work is completed. Also, it provides better battery performance, which is the key point in any application.
activity_main.xml
This tutorial deals with the Android JobShedular UI is easy. Just two buttons to start and stop jobscheduler.
;
;
MyJobScheduler.Java
Add Android JobScheduler, Job Scheduler starts from this class where we can find methods that will start and stop work.
This method deals with the job start where we can also initialize our MyJobExecutor.java class so that we can execute our task as the job starts.
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;
}
}