Skip to content

Commit e3d3154

Browse files
Dianne HackbornAndroid (Google) Code Review
authored andcommitted
Merge "Fix issue #29058724: Improve JobScheduler API demo" into nyc-dev
2 parents d50f287 + 1f50254 commit e3d3154

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2016, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.apis.content;
18+
19+
/**
20+
* Global constant job IDs used by the app.
21+
*/
22+
public final class JobIds {
23+
static final int MEDIA_CONTENT_JOB = 1;
24+
static final int PHOTOS_CONTENT_JOB = 2;
25+
}

samples/ApiDemos/src/com/example/android/apis/content/MediaContentJob.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.List;
3434

3535
/**
36-
* Stub job to execute when there is a change to any media: content URI.
36+
* Example stub job to monitor when there is a change to any media: content URI.
3737
*/
3838
public class MediaContentJob extends JobService {
3939
static final Uri MEDIA_URI = Uri.parse("content://" + MediaStore.AUTHORITY + "/");
@@ -50,7 +50,7 @@ public class MediaContentJob extends JobService {
5050

5151
public static void scheduleJob(Context context) {
5252
JobScheduler js = context.getSystemService(JobScheduler.class);
53-
JobInfo.Builder builder = new JobInfo.Builder(R.id.schedule_media_job,
53+
JobInfo.Builder builder = new JobInfo.Builder(JobIds.MEDIA_CONTENT_JOB,
5454
new ComponentName(context, MediaContentJob.class));
5555
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MEDIA_URI,
5656
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
@@ -65,7 +65,7 @@ public static boolean isScheduled(Context context) {
6565
return false;
6666
}
6767
for (int i=0; i<jobs.size(); i++) {
68-
if (jobs.get(i).getId() == R.id.schedule_media_job) {
68+
if (jobs.get(i).getId() == JobIds.MEDIA_CONTENT_JOB) {
6969
return true;
7070
}
7171
}
@@ -74,7 +74,7 @@ public static boolean isScheduled(Context context) {
7474

7575
public static void cancelJob(Context context) {
7676
JobScheduler js = context.getSystemService(JobScheduler.class);
77-
js.cancel(R.id.schedule_media_job);
77+
js.cancel(JobIds.MEDIA_CONTENT_JOB);
7878
}
7979

8080
@Override

samples/ApiDemos/src/com/example/android/apis/content/PhotosContentJob.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
package com.example.android.apis.content;
1818

19+
import com.example.android.apis.R;
20+
21+
//BEGIN_INCLUDE(job)
1922
import android.app.job.JobInfo;
2023
import android.app.job.JobParameters;
2124
import android.app.job.JobScheduler;
@@ -30,13 +33,11 @@
3033
import android.util.Log;
3134
import android.widget.Toast;
3235

33-
import com.example.android.apis.R;
34-
3536
import java.util.ArrayList;
3637
import java.util.List;
3738

3839
/**
39-
* Stub job to execute when there is a change to photos in the media provider.
40+
* Example stub job to monitor when there is a change to photos in the media provider.
4041
*/
4142
public class PhotosContentJob extends JobService {
4243
// The root URI of the media provider, to monitor for generic changes to its content.
@@ -61,7 +62,7 @@ public class PhotosContentJob extends JobService {
6162
static final JobInfo JOB_INFO;
6263

6364
static {
64-
JobInfo.Builder builder = new JobInfo.Builder(R.id.schedule_photos_job,
65+
JobInfo.Builder builder = new JobInfo.Builder(JobIds.PHOTOS_CONTENT_JOB,
6566
new ComponentName("com.example.android.apis", PhotosContentJob.class.getName()));
6667
// Look for specific changes to images in the provider.
6768
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(
@@ -98,7 +99,7 @@ public static boolean isScheduled(Context context) {
9899
return false;
99100
}
100101
for (int i=0; i<jobs.size(); i++) {
101-
if (jobs.get(i).getId() == R.id.schedule_photos_job) {
102+
if (jobs.get(i).getId() == JobIds.PHOTOS_CONTENT_JOB) {
102103
return true;
103104
}
104105
}
@@ -108,7 +109,7 @@ public static boolean isScheduled(Context context) {
108109
// Cancel this job, if currently scheduled.
109110
public static void cancelJob(Context context) {
110111
JobScheduler js = context.getSystemService(JobScheduler.class);
111-
js.cancel(R.id.schedule_photos_job);
112+
js.cancel(JobIds.PHOTOS_CONTENT_JOB);
112113
}
113114

114115
@Override
@@ -209,3 +210,4 @@ public boolean onStopJob(JobParameters params) {
209210
return false;
210211
}
211212
}
213+
//END_INCLUDE(job)

0 commit comments

Comments
 (0)