Merge "Fix issue #29058724: Improve JobScheduler API demo" into nyc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
e3d3154c1a
@@ -0,0 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2016, 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.apis.content;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Global constant job IDs used by the app.
|
||||||
|
*/
|
||||||
|
public final class JobIds {
|
||||||
|
static final int MEDIA_CONTENT_JOB = 1;
|
||||||
|
static final int PHOTOS_CONTENT_JOB = 2;
|
||||||
|
}
|
||||||
@@ -33,7 +33,7 @@ import com.example.android.apis.R;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stub job to execute when there is a change to any media: content URI.
|
* Example stub job to monitor when there is a change to any media: content URI.
|
||||||
*/
|
*/
|
||||||
public class MediaContentJob extends JobService {
|
public class MediaContentJob extends JobService {
|
||||||
static final Uri MEDIA_URI = Uri.parse("content://" + MediaStore.AUTHORITY + "/");
|
static final Uri MEDIA_URI = Uri.parse("content://" + MediaStore.AUTHORITY + "/");
|
||||||
@@ -50,7 +50,7 @@ public class MediaContentJob extends JobService {
|
|||||||
|
|
||||||
public static void scheduleJob(Context context) {
|
public static void scheduleJob(Context context) {
|
||||||
JobScheduler js = context.getSystemService(JobScheduler.class);
|
JobScheduler js = context.getSystemService(JobScheduler.class);
|
||||||
JobInfo.Builder builder = new JobInfo.Builder(R.id.schedule_media_job,
|
JobInfo.Builder builder = new JobInfo.Builder(JobIds.MEDIA_CONTENT_JOB,
|
||||||
new ComponentName(context, MediaContentJob.class));
|
new ComponentName(context, MediaContentJob.class));
|
||||||
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MEDIA_URI,
|
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MEDIA_URI,
|
||||||
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
|
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
|
||||||
@@ -65,7 +65,7 @@ public class MediaContentJob extends JobService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (int i=0; i<jobs.size(); i++) {
|
for (int i=0; i<jobs.size(); i++) {
|
||||||
if (jobs.get(i).getId() == R.id.schedule_media_job) {
|
if (jobs.get(i).getId() == JobIds.MEDIA_CONTENT_JOB) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -74,7 +74,7 @@ public class MediaContentJob extends JobService {
|
|||||||
|
|
||||||
public static void cancelJob(Context context) {
|
public static void cancelJob(Context context) {
|
||||||
JobScheduler js = context.getSystemService(JobScheduler.class);
|
JobScheduler js = context.getSystemService(JobScheduler.class);
|
||||||
js.cancel(R.id.schedule_media_job);
|
js.cancel(JobIds.MEDIA_CONTENT_JOB);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
package com.example.android.apis.content;
|
package com.example.android.apis.content;
|
||||||
|
|
||||||
|
import com.example.android.apis.R;
|
||||||
|
|
||||||
|
//BEGIN_INCLUDE(job)
|
||||||
import android.app.job.JobInfo;
|
import android.app.job.JobInfo;
|
||||||
import android.app.job.JobParameters;
|
import android.app.job.JobParameters;
|
||||||
import android.app.job.JobScheduler;
|
import android.app.job.JobScheduler;
|
||||||
@@ -30,13 +33,11 @@ import android.provider.MediaStore;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.example.android.apis.R;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stub job to execute when there is a change to photos in the media provider.
|
* Example stub job to monitor when there is a change to photos in the media provider.
|
||||||
*/
|
*/
|
||||||
public class PhotosContentJob extends JobService {
|
public class PhotosContentJob extends JobService {
|
||||||
// The root URI of the media provider, to monitor for generic changes to its content.
|
// The root URI of the media provider, to monitor for generic changes to its content.
|
||||||
@@ -61,7 +62,7 @@ public class PhotosContentJob extends JobService {
|
|||||||
static final JobInfo JOB_INFO;
|
static final JobInfo JOB_INFO;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
JobInfo.Builder builder = new JobInfo.Builder(R.id.schedule_photos_job,
|
JobInfo.Builder builder = new JobInfo.Builder(JobIds.PHOTOS_CONTENT_JOB,
|
||||||
new ComponentName("com.example.android.apis", PhotosContentJob.class.getName()));
|
new ComponentName("com.example.android.apis", PhotosContentJob.class.getName()));
|
||||||
// Look for specific changes to images in the provider.
|
// Look for specific changes to images in the provider.
|
||||||
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(
|
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(
|
||||||
@@ -98,7 +99,7 @@ public class PhotosContentJob extends JobService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
for (int i=0; i<jobs.size(); i++) {
|
for (int i=0; i<jobs.size(); i++) {
|
||||||
if (jobs.get(i).getId() == R.id.schedule_photos_job) {
|
if (jobs.get(i).getId() == JobIds.PHOTOS_CONTENT_JOB) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,7 +109,7 @@ public class PhotosContentJob extends JobService {
|
|||||||
// Cancel this job, if currently scheduled.
|
// Cancel this job, if currently scheduled.
|
||||||
public static void cancelJob(Context context) {
|
public static void cancelJob(Context context) {
|
||||||
JobScheduler js = context.getSystemService(JobScheduler.class);
|
JobScheduler js = context.getSystemService(JobScheduler.class);
|
||||||
js.cancel(R.id.schedule_photos_job);
|
js.cancel(JobIds.PHOTOS_CONTENT_JOB);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -209,3 +210,4 @@ public class PhotosContentJob extends JobService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//END_INCLUDE(job)
|
||||||
|
|||||||
Reference in New Issue
Block a user