Processes above JobService and jobscheduler Android 5.0 are alive

Processes above JobService and jobscheduler Android 5.0 are alive

Android 5.0 adds a new job scheduler API that allows you to define jobs that the system runs asynchronously at a later time or under specified conditions, such as when the device is charging, to optimize battery life. The job scheduling function is useful when:

• applications have jobs that are not user oriented and can be postponed
 • applications have jobs that you prefer to do when the device is plugged in
 • the application has a task to access the network or connect to WLAN.
• applications have multiple tasks that you want to run on a regular basis in batches.

demo is as follows:

1. Create a custom Service to inherit JobService

public class MyJobService extends JobService {

Override the onStartJob method and return true to indicate execution is in progress. Here, the task that takes time to execute is simulated

public boolean onStartJob(JobParameters params) {
    Log.e(TAG, "onStartJob  jobId = " + params.getJobId());
    new SimpleTask().execute(params);
    return true;
private class SimpleTask extends AsyncTask<JobParameters, Void, String> {

    private JobParameters mJobParam;

    protected String doInBackground(JobParameters... params) {
        mJobParam = params[0];
        sendMessage(MainActivity.MSG_JOB_START, mJobParam);
        try {
            return new String("End of task execution--->");
        } catch (Exception e) {
            return "ERROR";

    protected void onPostExecute(String result) {
        jobFinished(mJobParam, false); // Manual end
        Log.e(TAG, "Get results:" + result);
        sendMessage(MainActivity.MSG_JOB_STOP, mJobParam);

2. Declare in the Manifest:

  android:permission="android.permission.BIND_JOB_SERVICE" />

3. Start the task in activity

private void init() {
    mHandler = new IncomingMessageHandler(this);
    Intent startServiceIntent = new Intent(this, MyJobService.class);
    Messenger messengerIncoming = new Messenger(mHandler);
    startServiceIntent.putExtra("messenger", messengerIncoming);

    mJobScheduler = (JobScheduler)     

There are two ways to do this. It should be noted that the Periodic way is to execute once every 15 minutes, while the other way is to execute immediately

private void startJob(boolean isPeriodic) {
    ComponentName jobService = new ComponentName(this, MyJobService.class);
    JobInfo.Builder builder = new JobInfo.Builder(jobId++, jobService);

    if (isPeriodic) {
    } else {
        builder.setOverrideDeadline(1000 * 30);

    // Set network conditions
    builder.setRequiresCharging(false);    // Set whether to charge
    builder.setRequiresDeviceIdle(false);  // Set whether the phone is idle
    JobInfo jobInfo =;

DEMO Description: click start with persistent to execute the task every 15 minutes, and click start with deadlock to start immediately

DEMO download address:

Tags: Android network

Posted on Mon, 04 May 2020 17:47:47 -0700 by anushka