Skip to content

Most visited

Recently visited

navigation
JobScheduler / src / com.example.android.jobscheduler / service /

TestJobService.java

1
/*
2
 * Copyright 2014 Google Inc.
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.jobscheduler.service;
18
 
19
import android.app.job.JobInfo;
20
import android.app.job.JobScheduler;
21
import android.app.job.JobParameters;
22
import android.app.job.JobService;
23
import android.content.Context;
24
import android.content.Intent;
25
import android.os.Message;
26
import android.os.Messenger;
27
import android.os.RemoteException;
28
import android.util.Log;
29
 
30
import com.example.android.jobscheduler.MainActivity;
31
 
32
import java.util.LinkedList;
33
 
34
 
35
/**
36
 * Service to handle callbacks from the JobScheduler. Requests scheduled with the JobScheduler
37
 * ultimately land on this service's "onStartJob" method. Currently all this does is post a message
38
 * to the app's main activity to change the state of the UI.
39
 */
40
public class TestJobService extends JobService {
41
    private static final String TAG = "SyncService";
42
 
43
    @Override
44
    public void onCreate() {
45
        super.onCreate();
46
        Log.i(TAG, "Service created");
47
    }
48
 
49
    @Override
50
    public void onDestroy() {
51
        super.onDestroy();
52
        Log.i(TAG, "Service destroyed");
53
    }
54
 
55
    /**
56
     * When the app's MainActivity is created, it starts this service. This is so that the
57
     * activity and this service can communicate back and forth. See "setUiCalback()"
58
     */
59
    @Override
60
    public int onStartCommand(Intent intent, int flags, int startId) {
61
        Messenger callback = intent.getParcelableExtra("messenger");
62
        Message m = Message.obtain();
63
        m.what = MainActivity.MSG_SERVICE_OBJ;
64
        m.obj = this;
65
        try {
66
            callback.send(m);
67
        } catch (RemoteException e) {
68
            Log.e(TAG, "Error passing service object back to activity.");
69
        }
70
        return START_NOT_STICKY;
71
    }
72
 
73
    @Override
74
    public boolean onStartJob(JobParameters params) {
75
        // We don't do any real 'work' in this sample app. All we'll
76
        // do is track which jobs have landed on our service, and
77
        // update the UI accordingly.
78
        jobParamsMap.add(params);
79
        if (mActivity != null) {
80
            mActivity.onReceivedStartJob(params);
81
        }
82
        Log.i(TAG, "on start job: " + params.getJobId());
83
        return true;
84
    }
85
 
86
    @Override
87
    public boolean onStopJob(JobParameters params) {
88
        // Stop tracking these job parameters, as we've 'finished' executing.
89
        jobParamsMap.remove(params);
90
        if (mActivity != null) {
91
            mActivity.onReceivedStopJob();
92
        }
93
        Log.i(TAG, "on stop job: " + params.getJobId());
94
        return true;
95
    }
96
 
97
    MainActivity mActivity;
98
    private final LinkedList<JobParameters> jobParamsMap = new LinkedList<JobParameters>();
99
 
100
    public void setUiCallback(MainActivity activity) {
101
        mActivity = activity;
102
    }
103
 
104
    /** Send job to the JobScheduler. */
105
    public void scheduleJob(JobInfo t) {
106
        Log.d(TAG, "Scheduling job");
107
        JobScheduler tm =
108
                (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
109
        tm.schedule(t);
110
    }
111
 
112
    /**
113
     * Not currently used, but as an exercise you can hook this
114
     * up to a button in the UI to finish a job that has landed
115
     * in onStartJob().
116
     */
117
    public boolean callJobFinished() {
118
        JobParameters params = jobParamsMap.poll();
119
        if (params == null) {
120
            return false;
121
        } else {
122
            jobFinished(params, false);
123
            return true;
124
        }
125
    }
126
 
127
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a one-minute survey?
Help us improve Android tools and documentation.