Job Scheduler在不到21个API中

时间:2016-01-10 03:16:41

标签: android scheduler jobs

我正在尝试创建一个调度程序,可以向用户发出通知,并在条件匹配时自动启动应用程序,并且该条件必须是用户选择的时间。 例如,您可以选择从现在开始4小时的时间,从现在起4小时后应用程序应该自行启动 我正在尝试教程here! 但我无法在API 21以下的任何地方运行它 但我的应用程序的最小sdk是19 这是我的代码

MainActivity.java

package com.example.jobscheduler;

import android.app.Activity;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends Activity {

    public static final int MSG_UNCOLOUR_START = 0;
      public static final int MSG_UNCOLOUR_STOP = 1;
      public static final int MSG_SERVICE_OBJ = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         Resources res = getResources();
//          defaultColor = res.getColor();
//          startJobColor = res.getColor(R.color.start_received);
//          stopJobColor = res.getColor(R.color.stop_received);

            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);
            mServiceComponent = new ComponentName(this, TestJobService.class);

    }

    int defaultColor;
      int startJobColor;
      int stopJobColor;
      private EditText mDelayEditText;
      private EditText mDeadlineEditText;
      private RadioButton mWiFiConnectivityRadioButton;
      private RadioButton mAnyConnectivityRadioButton;
      private CheckBox mRequiresChargingCheckBox;
      private CheckBox mRequiresIdleCheckbox;
      ComponentName mServiceComponent;


      TestJobService mTestService;
      private static int kJobId = 0;


    /**
       * UI onclick listener to schedule a new job. 
       */

      public void scheduleJob(View v) {
        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());
        JobScheduler jobScheduler =
                (JobScheduler) getApplication().getSystemService(Context.JOB_SCHEDULER_SERVICE);

        jobScheduler.schedule(builder.build());
      }

      public void cancelAllJobs(View v) {
        JobScheduler tm = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        tm.cancelAll();
      }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}




    package com.example.jobscheduler;
import java.util.LinkedList;

import android.app.job.JobParameters;
import android.app.job.JobService;
import android.util.Log;

/**
 * JobService to be scheduled by the JobScheduler. Requests scheduled with
 * the JobScheduler ultimately land on this service's "onStartJob" method.
 * Currently all this does is write a log entry
 */

public class TestJobService extends JobService {
  private static final String TAG = "SyncService";


  @Override
  public boolean onStartJob(JobParameters params) {
    // We don't do any real 'work' in this sample app. All we'll
    // do is track which jobs have landed on our service, and
    // update the UI accordingly.
    Log.i(TAG, "on start job: " + params.getJobId());
    return true;
  }

  @Override
  public boolean onStopJob(JobParameters params) {
    Log.i(TAG, "on stop job: " + params.getJobId());
    return true;
  }

  MainActivity mActivity;
  private final LinkedList<JobParameters> jobParamsMap = new LinkedList<JobParameters>();

  public void setUiCallback(MainActivity activity) {
    mActivity = activity;
  }

} 

0 个答案:

没有答案