Home | History | Annotate | Download | only in app
      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
     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)).setOverrideDeadline(0).build();
     46 
     47         setContentView(R.layout.job_work_service_activity);
     48 
     49         // Watch for button clicks.
     50         Button button = findViewById(R.id.enqueue1);
     51         button.setOnClickListener(mEnqueue1Listener);
     52         button = findViewById(R.id.enqueue2);
     53         button.setOnClickListener(mEnqueue2Listener);
     54         button = findViewById(R.id.enqueue3);
     55         button.setOnClickListener(mEnqueue3Listener);
     56         button = findViewById(R.id.kill);
     57         button.setOnClickListener(mKillListener);
     58     }
     59 
     60     private View.OnClickListener mEnqueue1Listener = new View.OnClickListener() {
     61         public void onClick(View v) {
     62             mJobScheduler.enqueue(mJobInfo, new JobWorkItem(
     63                     new Intent("com.example.android.apis.ONE").putExtra("name", "One")));
     64         }
     65     };
     66 
     67     private View.OnClickListener mEnqueue2Listener = new View.OnClickListener() {
     68         public void onClick(View v) {
     69             mJobScheduler.enqueue(mJobInfo, new JobWorkItem(
     70                     new Intent("com.example.android.apis.TWO").putExtra("name", "Two")));
     71         }
     72     };
     73 
     74     private View.OnClickListener mEnqueue3Listener = new View.OnClickListener() {
     75         public void onClick(View v) {
     76             mJobScheduler.enqueue(mJobInfo, new JobWorkItem(
     77                     new Intent("com.example.android.apis.THREE").putExtra("name", "Three")));
     78         }
     79     };
     80 
     81     private View.OnClickListener mKillListener = new View.OnClickListener() {
     82         public void onClick(View v) {
     83             // This is to simulate the service being killed while it is
     84             // running in the background.
     85             Process.killProcess(Process.myPid());
     86         }
     87     };
     88 }
     89