FileDocCategorySizeDatePackage
MainActivity.javaAPI DocAndroid 5.1 API7075Thu Mar 12 22:22:44 GMT 2015com.android.demo.jobSchedulerApp

MainActivity

public class MainActivity extends android.app.Activity

Fields Summary
private static final String
TAG
public static final int
MSG_UNCOLOUR_START
public static final int
MSG_UNCOLOUR_STOP
public static final int
MSG_SERVICE_OBJ
int
defaultColor
int
startJobColor
int
stopJobColor
android.widget.TextView
mShowStartView
android.widget.TextView
mShowStopView
android.widget.TextView
mParamsTextView
android.widget.EditText
mDelayEditText
android.widget.EditText
mDeadlineEditText
android.widget.RadioButton
mWiFiConnectivityRadioButton
android.widget.RadioButton
mAnyConnectivityRadioButton
android.widget.CheckBox
mRequiresChargingCheckBox
android.widget.CheckBox
mRequiresIdleCheckbox
android.widget.CheckBox
mIsPersistedCheckbox
android.content.ComponentName
mServiceComponent
com.android.demo.jobSchedulerApp.service.TestJobService
mTestService
Service object to interact scheduled jobs.
private static int
kJobId
android.os.Handler
mHandler
Constructors Summary
Methods Summary
public voidcancelAllJobs(android.view.View v)

        JobScheduler tm =
                (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        tm.cancelAll();
    
private booleanensureTestService()


       
        if (mTestService == null) {
            Toast.makeText(MainActivity.this, "Service null, never got callback?",
                    Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    
public voidfinishJob(android.view.View v)
UI onclick listener to call jobFinished() in our service.

        if (!ensureTestService()) {
            return;
        }
        mTestService.callJobFinished();
        mParamsTextView.setText("");
    
public voidonCreate(android.os.Bundle savedInstanceState)


    
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Resources res = getResources();
        defaultColor = res.getColor(R.color.none_received);
        startJobColor = res.getColor(R.color.start_received);
        stopJobColor = res.getColor(R.color.stop_received);

        // Set up UI.
        mShowStartView = (TextView) findViewById(R.id.onstart_textview);
        mShowStopView = (TextView) findViewById(R.id.onstop_textview);
        mParamsTextView = (TextView) findViewById(R.id.task_params);
        mDelayEditText = (EditText) findViewById(R.id.delay_time);
        mDeadlineEditText = (EditText) findViewById(R.id.deadline_time);
        mWiFiConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_unmetered);
        mAnyConnectivityRadioButton = (RadioButton) findViewById(R.id.checkbox_any);
        mRequiresChargingCheckBox = (CheckBox) findViewById(R.id.checkbox_charging);
        mRequiresIdleCheckbox = (CheckBox) findViewById(R.id.checkbox_idle);
        mIsPersistedCheckbox = (CheckBox) findViewById(R.id.checkbox_persisted);

        mServiceComponent = new ComponentName(this, TestJobService.class);
        // Start service and provide it a way to communicate with us.
        Intent startServiceIntent = new Intent(this, TestJobService.class);
        startServiceIntent.putExtra("messenger", new Messenger(mHandler));
        startService(startServiceIntent);
    
public voidonReceivedStartJob(android.app.job.JobParameters params)

        mShowStartView.setBackgroundColor(startJobColor);
        Message m = Message.obtain(mHandler, MSG_UNCOLOUR_START);
        mHandler.sendMessageDelayed(m, 1000L); // uncolour in 1 second.
        mParamsTextView.setText("Executing: " + params.getJobId() + " " + params.getExtras());
    
public voidonReceivedStopJob()

        mShowStopView.setBackgroundColor(stopJobColor);
        Message m = Message.obtain(mHandler, MSG_UNCOLOUR_STOP);
        mHandler.sendMessageDelayed(m, 2000L); // uncolour in 1 second.
        mParamsTextView.setText("");
    
public voidscheduleJob(android.view.View v)
UI onclick listener to schedule a job. What this job is is defined in TestJobService#scheduleJob()

        if (!ensureTestService()) {
            return;
        }

        JobInfo.Builder builder = new JobInfo.Builder(kJobId++, mServiceComponent);

        String delay = mDelayEditText.getText().toString();
        if (delay != null && !TextUtils.isEmpty(delay)) {
            builder.setMinimumLatency(Long.valueOf(delay) * 1000);
        }
        String deadline = mDeadlineEditText.getText().toString();
        if (deadline != null && !TextUtils.isEmpty(deadline)) {
            builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);
        }
        boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
        boolean requiresAnyConnectivity = mAnyConnectivityRadioButton.isChecked();
        if (requiresUnmetered) {
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
        } else if (requiresAnyConnectivity) {
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        }
        builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
        builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
        builder.setPersisted(mIsPersistedCheckbox.isChecked());
        mTestService.scheduleJob(builder.build());