1. Usage scenarios

To do something under certain conditions, such as the device in idle state, and using wifi, automatically download a new Apk.

Since 5.0, Android has added support for a special mechanism, namely JobScheduler, which integrates several common operating conditions. Developers only need to add a few lines of code to complete the work that used to require multiple components, making the code more elegant.

  1. Prerequisite for

JobScheduler: Task scheduler

JobInfo: indicates the task summary

JobService: describes the specific logic of the task service

  1. How to use

    1. Create your own JobService

      Inheritance JobSerive

      OnStartJob: Triggered when a task starts to be executed. Return false to indicate completion, and true to indicate completion by calling the jobFinished method.

      OnStopJob: Triggered when a task is stopped.

    2. Activity onStart Creates a Service

         Intent startServiceIntent = new Intent(this, MyJobService.class);
         Messenger messengerIncoming = new Messenger(mHandler);
         startServiceIntent.putExtra(MESSENGER_INTENT_KEY, messengerIncoming);
         startService(startServiceIntent);
    Copy the code
    1. Configure JobInfo in the Activity

    JobInfo.Builder builder = new JobInfo.Builder(mJobId++, mServiceComponent);

    String delay = mDelayEditText.getText().toString(); if (! TextUtils. IsEmpty (delay)) {/ / set at least how Long delay after execution, the unit milliseconds. Builder. SetMinimumLatency (Long) the valueOf (delay) * 1000); } String deadline = mDeadlineEditText.getText().toString(); if (! Textutils.isempty (deadline)) {// Sets the maximum delay for execution in milliseconds. builder.setOverrideDeadline(Long.valueOf(deadline) * 1000); } boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked(); boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked(); RequiresUnmetered {if (requiresUnmetered) {// Set the required network condition. //JobInfo.NETWORK_TYPE_NONE (executed when there is no network, Default), //JobInfo.NETWORK_TYPE_ANY (when there is a network), //JobInfo.NETWORK_TYPE_UNMETERED (when there is no network charge) builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED); } else if (requiresAnyConnectivity) { builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); } / / whether the execution builder. In your spare time setRequiresDeviceIdle (mRequiresIdleCheckbox. IsChecked ()); / / is in the charge when the builder. SetRequiresCharging (mRequiresChargingCheckBox. IsChecked ()); // Extras, work duration. PersistableBundle extras = new PersistableBundle(); String workDuration = mDurationTimeEditText.getText().toString(); if (TextUtils.isEmpty(workDuration)) { workDuration = "1"; } extras.putLong(WORK_DURATION_KEY, Long.valueOf(workDuration) * 1000); builder.setExtras(extras); // Schedule job Log.d(TAG, "Scheduling job"); JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); tm.schedule(builder.build());Copy the code
    1. OnStartJob and onStopJob are both called by the main thread, so long tasks need child threads to operate!