4

i'm trying to make a scheduler that can give a notification to the user and start the app automatically when the condition is matched and that condition has to be a time that user selects. So for example you can select time 4 hours from now and exactly 4 hours from now app should launch itself i'm trying a tutorial here! but i cannot run it on anything below API 21 but my app's minimum sdk is 19 here is my code

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;
  }

} 
ravi raghav
  • 85
  • 11

0 Answers0