[embedyt] https://www.youtube.com/watch?v=3EQWmME-hNA[/embedyt]
activity_main.xmlMainActivity.javaExampleJobService.javaAndroidManifest.xml
<?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" android:gravity="center" android:orientation="vertical" tools:context="com.codinginflow.jobschedulerexample.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="scheduleJob" android:text="Schedule Job" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="cancelJob" android:text="Cancel Job" /> </LinearLayout>
package com.codinginflow.jobschedulerexample; import android.app.job.JobInfo; import android.app.job.JobScheduler; import android.content.ComponentName; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void scheduleJob(View v) { ComponentName componentName = new ComponentName(this, ExampleJobService.class); JobInfo info = new JobInfo.Builder(123, componentName) .setRequiresCharging(true) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .setPersisted(true) .setPeriodic(15 * 60 * 1000) .build(); JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); int resultCode = scheduler.schedule(info); if (resultCode == JobScheduler.RESULT_SUCCESS) { Log.d(TAG, "Job scheduled"); } else { Log.d(TAG, "Job scheduling failed"); } } public void cancelJob(View v) { JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE); scheduler.cancel(123); Log.d(TAG, "Job cancelled"); } }
package com.codinginflow.jobschedulerexample; import android.app.job.JobParameters; import android.app.job.JobService; import android.util.Log; public class ExampleJobService extends JobService { private static final String TAG = "ExampleJobService"; private boolean jobCancelled = false; @Override public boolean onStartJob(JobParameters params) { Log.d(TAG, "Job started"); doBackgroundWork(params); return true; } private void doBackgroundWork(final JobParameters params) { new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { Log.d(TAG, "run: " + i); if (jobCancelled) { return; } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } Log.d(TAG, "Job finished"); jobFinished(params, false); } }).start(); } @Override public boolean onStopJob(JobParameters params) { Log.d(TAG, "Job cancelled before completion"); jobCancelled = true; return true; } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.codinginflow.jobschedulerexample"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".ExampleJobService" android:permission="android.permission.BIND_JOB_SERVICE" /> </application> </manifest>