/* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.supportv4.app; //BEGIN_INCLUDE(complete) import android.content.Context; import android.content.Intent; import android.os.Handler; import android.os.SystemClock; import android.support.v4.app.JobIntentService; import android.util.Log; import android.widget.Toast; /** * Example implementation of a JobIntentService. */ public class SimpleJobIntentService extends JobIntentService { /** * Unique job ID for this service. */ static final int JOB_ID = 1000; /** * Convenience method for enqueuing work in to this service. */ static void enqueueWork(Context context, Intent work) { enqueueWork(context, SimpleJobIntentService.class, JOB_ID, work); } @Override protected void onHandleWork(Intent intent) { // We have received work to do. The system or framework is already // holding a wake lock for us at this point, so we can just go. Log.i("SimpleJobIntentService", "Executing work: " + intent); String label = intent.getStringExtra("label"); if (label == null) { label = intent.toString(); } toast("Executing: " + label); for (int i = 0; i < 5; i++) { Log.i("SimpleJobIntentService", "Running service " + (i + 1) + "/5 @ " + SystemClock.elapsedRealtime()); try { Thread.sleep(1000); } catch (InterruptedException e) { } } Log.i("SimpleJobIntentService", "Completed service @ " + SystemClock.elapsedRealtime()); } @Override public void onDestroy() { super.onDestroy(); toast("All work complete"); } final Handler mHandler = new Handler(); // Helper for showing tests void toast(final CharSequence text) { mHandler.post(new Runnable() { @Override public void run() { Toast.makeText(SimpleJobIntentService.this, text, Toast.LENGTH_SHORT).show(); } }); } } //END_INCLUDE(complete)