Basic usage

suggest change

Create a new JobService

This is done by extending the JobService class and implementing/overriding the required methods onStartJob() and onStopJob().

public class MyJobService extends JobService
{
    final String TAG = getClass().getSimpleName();
    
    @Override
    public boolean onStartJob(JobParameters jobParameters) {
        Log.i(TAG, "Job started");

        // ... your code here, still on the main thread ...

        return false; // The work is already finished, so do not call jobFinished.
    }

    @Override
    public boolean onStopJob(JobParameters jobParameters) {
        Log.w(TAG, "Job stopped");
        return false;
    }
}

Add the new JobService to your AndroidManifest.xml

The following step is mandatory. Otherwise you will not be able to run your job:

Declare your MyJobService class as a new <service> element between <application> </application> in your AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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=".MyJobService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="true" />
    </application>
</manifest>

Setup and run the job

After you implemented a new JobService and added it to your AndroidManifest.xml, you can continue with the final steps.

public class MainActivity extends AppCompatActivity
{
    final String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onButtonClick_startJob(View v) {
        // get the jobScheduler instance from current context
        JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);

        // MyJobService provides the implementation for the job
        ComponentName jobService = new ComponentName(getApplicationContext(), MyJobService.class);

        // Request a 10 second period. From Android 7.0, the platform raises this to at least 15 minutes.
        JobInfo jobInfo = new JobInfo.Builder(1, jobService).setPeriodic(10 * 1000).build();

        // schedule/start the job
        int result = jobScheduler.schedule(jobInfo);
        if (result == JobScheduler.RESULT_SUCCESS)
            Log.d(TAG, "Successfully scheduled job: " + result);
        else
            Log.e(TAG, "RESULT_FAILURE: " + result);
    }

    public void onButtonClick_stopJob(View v) {
        JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
        Log.d(TAG, "Stopping all jobs...");
        jobScheduler.cancelAll(); // cancel all potentially running jobs
    }
}

After onButtonClick_startJob(), the job is scheduled while the app is in the background (for example after the user presses Home). On Android 7.0 and later it does not actually run every 10 seconds. The minimum periodic interval is 15 minutes.

Instead of cancelling every job in onButtonClick_stopJob(), call jobScheduler.cancel(1) to cancel the job whose id is 1.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents