JobScheduler 5.0API after the use of GCM before 5.0API – Google Play ServiceManager, is basically obsolete in China. (There is a lot of network optimization code in it – including access policies, etc.)
package com.nan.batterydemo;
import android.annotation.TargetApi; import android.app.AlarmManager; import android.app.PendingIntent; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.os.BatteryManager; import android.os.Build; import android.os.Bundle; import android.os.PowerManager; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.WindowManager;
import java.util.List;
@TargetApi(21) public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName(); @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ComponentName serviceComponent = new ComponentName(this, MyJobService.class); JobScheduler = (JobScheduler) getSystemService(context.job_scheduler_service); for (int i = 0; i < 500; i++) { JobInfo jobInfo = new JobInfo.Builder(i, ServiceComponent).setMinimumLatency(5000)// Minimum delay 5 seconds. SetOverrideDeadline (60000)// Maximum execution time 60 secondsCopy the code
// setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)// Free network — WIFI Bluetooth USB .setrequiredNetworkType (JobInfo.NETWORK_TYPE_ANY)// Any network –wifi // Set retry policy: When a mission fails, What retry policy is adopted // Wait interval for the first retry attempt. SetBackoffCriteria (jobinfo.default_initial_backoff_millis, Jobinfo.backoff_policy_linear) // set the task to be persisted after the device restarts. jobScheduler.schedule(jobInfo);
Jobscheduler.cancel (1); // Cancel the job. // Cancel job scheduler. CancelAll (); / / get the process inside all waiting for the task List of < the JobInfo > allPendingJobs = jobScheduler. GetAllPendingJobs (); }}Copy the code
}