1 /*
2  * Copyright (C) 2017 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.app;
18 
19 import android.app.Activity;
20 import android.app.job.JobInfo;
21 import android.app.job.JobScheduler;
22 import android.app.job.JobWorkItem;
23 import android.content.ComponentName;
24 import android.content.Intent;
25 import android.os.Bundle;
26 import android.os.Process;
27 import android.view.View;
28 import android.widget.Button;
29 
30 import com.example.android.apis.R;
31 
32 /**
33  * Example of interacting with {@link JobWorkService}.
34  */
35 public class JobWorkServiceActivity extends Activity {
36     JobScheduler mJobScheduler;
37     JobInfo mJobInfo;
38 
39     @Override
onCreate(Bundle savedInstanceState)40     protected void onCreate(Bundle savedInstanceState) {
41         super.onCreate(savedInstanceState);
42 
43         mJobScheduler = (JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
44         mJobInfo = new JobInfo.Builder(R.string.job_service_created,
45                 new ComponentName(this, JobWorkService.class))
46                 .setRequiresBatteryNotLow(true)
47                 .build();
48 
49         setContentView(R.layout.job_work_service_activity);
50 
51         // Watch for button clicks.
52         Button button = findViewById(R.id.enqueue1);
53         button.setOnClickListener(mEnqueue1Listener);
54         button = findViewById(R.id.enqueue2);
55         button.setOnClickListener(mEnqueue2Listener);
56         button = findViewById(R.id.enqueue3);
57         button.setOnClickListener(mEnqueue3Listener);
58         button = findViewById(R.id.kill);
59         button.setOnClickListener(mKillListener);
60     }
61 
62     private View.OnClickListener mEnqueue1Listener = new View.OnClickListener() {
63         public void onClick(View v) {
64             mJobScheduler.enqueue(mJobInfo, new JobWorkItem(
65                     new Intent("com.example.android.apis.ONE").putExtra("name", "One")));
66         }
67     };
68 
69     private View.OnClickListener mEnqueue2Listener = new View.OnClickListener() {
70         public void onClick(View v) {
71             mJobScheduler.enqueue(mJobInfo, new JobWorkItem(
72                     new Intent("com.example.android.apis.TWO").putExtra("name", "Two")));
73         }
74     };
75 
76     private View.OnClickListener mEnqueue3Listener = new View.OnClickListener() {
77         public void onClick(View v) {
78             mJobScheduler.enqueue(mJobInfo, new JobWorkItem(
79                     new Intent("com.example.android.apis.THREE").putExtra("name", "Three")));
80         }
81     };
82 
83     private View.OnClickListener mKillListener = new View.OnClickListener() {
84         public void onClick(View v) {
85             // This is to simulate the service being killed while it is
86             // running in the background.
87             Process.killProcess(Process.myPid());
88         }
89     };
90 }
91