Java Code Examples for android.app.job.JobInfo#NETWORK_TYPE_UNMETERED

The following examples show how to use android.app.job.JobInfo#NETWORK_TYPE_UNMETERED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BackgroundFetchConfig.java    From transistor-background-fetch with MIT License 6 votes vote down vote up
public Builder setRequiredNetworkType(int networkType) {

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
                if (
                    (networkType != JobInfo.NETWORK_TYPE_ANY) &&
                    (networkType != JobInfo.NETWORK_TYPE_CELLULAR) &&
                    (networkType != JobInfo.NETWORK_TYPE_NONE) &&
                    (networkType != JobInfo.NETWORK_TYPE_NOT_ROAMING) &&
                    (networkType != JobInfo.NETWORK_TYPE_UNMETERED)
                ) {
                    Log.e(BackgroundFetch.TAG, "[ERROR] Invalid " + FIELD_REQUIRED_NETWORK_TYPE + ": " + networkType + "; Defaulting to NETWORK_TYPE_NONE");
                    networkType = JobInfo.NETWORK_TYPE_NONE;
                }
                this.requiredNetworkType = networkType;
            }
            return this;
        }
 
Example 2
Source File: JobProxy21.java    From android-job with Apache License 2.0 6 votes vote down vote up
protected int convertNetworkType(@NonNull JobRequest.NetworkType networkType) {
    switch (networkType) {
        case ANY:
            return JobInfo.NETWORK_TYPE_NONE;
        case CONNECTED:
            return JobInfo.NETWORK_TYPE_ANY;
        case UNMETERED:
            return JobInfo.NETWORK_TYPE_UNMETERED;
        case NOT_ROAMING:
            return JobInfo.NETWORK_TYPE_UNMETERED; // use unmetered here, is overwritten in v24
        case METERED:
            return JobInfo.NETWORK_TYPE_ANY; // use any here as fallback
        default:
            throw new IllegalStateException("not implemented");
    }
}
 
Example 3
Source File: MainActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
public void scheduleJob(View view) {
    int selectedNetworkID = networkOptions.getCheckedRadioButtonId();
    int selectedNetworkOption = JobInfo.NETWORK_TYPE_NONE;
    int seekBarInteger = mSeekBar.getProgress();
    boolean seekBarSet = seekBarInteger > 0;
    switch (selectedNetworkID) {
        case R.id.noNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_NONE;
            break;
        case R.id.anyNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_ANY;
            break;
        case R.id.wifiNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_UNMETERED;
            break;
    }

    mScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    ComponentName componentName = new ComponentName(getPackageName(), NotificationJobService.class.getName());
    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName);
    builder.setRequiredNetworkType(selectedNetworkOption)
            .setRequiresDeviceIdle(mDeviceIdleSwitch.isChecked())
            .setRequiresCharging(mDeviceChargingSwitch.isChecked());
    if (seekBarSet){
        builder.setOverrideDeadline(seekBarInteger * 1000);
    }
    boolean constraintSet = (selectedNetworkOption != JobInfo.NETWORK_TYPE_NONE) || mDeviceChargingSwitch.isChecked() || mDeviceIdleSwitch.isChecked() || seekBarSet;
    if (constraintSet) {
        JobInfo myJobInfo = builder.build();
        mScheduler.schedule(myJobInfo);
        Toast.makeText(this, "Job Scheduled, job will run when " + "the constraints are met.", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Please set at least one constraint", Toast.LENGTH_SHORT).show();
    }
}
 
Example 4
Source File: MainActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
public void scheduleJob(View view) {
    int selectedNetworkID = networkOptions.getCheckedRadioButtonId();
    int selectedNetworkOption = JobInfo.NETWORK_TYPE_NONE;
    int seekBarInteger = mSeekBar.getProgress();
    boolean seekBarSet = seekBarInteger > 0;
    switch (selectedNetworkID) {
        case R.id.noNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_NONE;
            break;
        case R.id.anyNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_ANY;
            break;
        case R.id.wifiNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_UNMETERED;
            break;
    }

    mScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    ComponentName componentName = new ComponentName(getPackageName(), SleeperJobScheduler.class.getName());
    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName);
    builder.setRequiredNetworkType(selectedNetworkOption)
            .setRequiresDeviceIdle(mDeviceIdleSwitch.isChecked())
            .setRequiresCharging(mDeviceChargingSwitch.isChecked());
    if (seekBarSet){
        builder.setOverrideDeadline(seekBarInteger * 1000);
    }
    boolean constraintSet = (selectedNetworkOption != JobInfo.NETWORK_TYPE_NONE) || mDeviceChargingSwitch.isChecked() || mDeviceIdleSwitch.isChecked() || seekBarSet;
    if (constraintSet) {
        JobInfo myJobInfo = builder.build();
        mScheduler.schedule(myJobInfo);
        Toast.makeText(this, "Job Scheduled, job will run when " + "the constraints are met.", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Please set at least one constraint", Toast.LENGTH_SHORT).show();
    }
}
 
Example 5
Source File: NotificationScheduler.java    From Google-AAD-CheatSheet with MIT License 4 votes vote down vote up
public void scheduleJob(View v){
    RadioGroup networkOptions = findViewById(R.id.networkOptions);
    int selectedNetworkID=networkOptions.getCheckedRadioButtonId();
    int selectedNetworkOption=JobInfo.NETWORK_TYPE_NONE;
    switch(selectedNetworkID){
        case R.id.noNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_NONE;
            break;
        case R.id.anyNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_ANY;
            break;
        case R.id.wifiNetwork:
            selectedNetworkOption = JobInfo.NETWORK_TYPE_UNMETERED;
            break;
    }

    mScheduler=(JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);




    ComponentName serviceName = new ComponentName(getPackageName(),
            NotificationJobService.class.getName());
    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, serviceName);
    builder.setRequiredNetworkType(selectedNetworkOption);
    builder.setRequiresDeviceIdle(mDeviceIdleSwitch.isChecked());
    builder.setRequiresCharging(mDeviceChargingSwitch.isChecked());
    int seekBarInteger = mSeekBar.getProgress();
    boolean seekBarSet = seekBarInteger > 0;
    if (seekBarSet) {
        builder.setOverrideDeadline(seekBarInteger * 1000);//过了ddl就不run你的job了
    }
    boolean constraintSet = (selectedNetworkOption != JobInfo.NETWORK_TYPE_NONE)
            || mDeviceChargingSwitch.isChecked() || mDeviceIdleSwitch.isChecked()|| seekBarSet;
    if(constraintSet) {
        //Schedule the job and notify the user
        JobInfo myJobInfo = builder.build();
        mScheduler.schedule(myJobInfo);
        Toast.makeText(this, "Job Scheduled, job will run when " +
                "the constraints are met.", Toast.LENGTH_SHORT).show();
    }else {
        Toast.makeText(this, "Please set at least one constraint",
                Toast.LENGTH_SHORT).show();
    }
}
 
Example 6
Source File: PlatformScheduler.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("MissingPermission")
private static JobInfo buildJobInfo(
    int jobId,
    ComponentName jobServiceComponentName,
    Requirements requirements,
    String serviceAction,
    String servicePackage) {
  JobInfo.Builder builder = new JobInfo.Builder(jobId, jobServiceComponentName);

  int networkType;
  switch (requirements.getRequiredNetworkType()) {
    case Requirements.NETWORK_TYPE_NONE:
      networkType = JobInfo.NETWORK_TYPE_NONE;
      break;
    case Requirements.NETWORK_TYPE_ANY:
      networkType = JobInfo.NETWORK_TYPE_ANY;
      break;
    case Requirements.NETWORK_TYPE_UNMETERED:
      networkType = JobInfo.NETWORK_TYPE_UNMETERED;
      break;
    case Requirements.NETWORK_TYPE_NOT_ROAMING:
      if (Util.SDK_INT >= 24) {
        networkType = JobInfo.NETWORK_TYPE_NOT_ROAMING;
      } else {
        throw new UnsupportedOperationException();
      }
      break;
    case Requirements.NETWORK_TYPE_METERED:
      if (Util.SDK_INT >= 26) {
        networkType = JobInfo.NETWORK_TYPE_METERED;
      } else {
        throw new UnsupportedOperationException();
      }
      break;
    default:
      throw new UnsupportedOperationException();
  }

  builder.setRequiredNetworkType(networkType);
  builder.setRequiresDeviceIdle(requirements.isIdleRequired());
  builder.setRequiresCharging(requirements.isChargingRequired());
  builder.setPersisted(true);

  PersistableBundle extras = new PersistableBundle();
  extras.putString(KEY_SERVICE_ACTION, serviceAction);
  extras.putString(KEY_SERVICE_PACKAGE, servicePackage);
  extras.putInt(KEY_REQUIREMENTS, requirements.getRequirementsData());
  builder.setExtras(extras);

  return builder.build();
}
 
Example 7
Source File: PlatformScheduler.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("MissingPermission")
private static JobInfo buildJobInfo(
    int jobId,
    ComponentName jobServiceComponentName,
    Requirements requirements,
    String serviceAction,
    String servicePackage) {
  JobInfo.Builder builder = new JobInfo.Builder(jobId, jobServiceComponentName);

  int networkType;
  switch (requirements.getRequiredNetworkType()) {
    case Requirements.NETWORK_TYPE_NONE:
      networkType = JobInfo.NETWORK_TYPE_NONE;
      break;
    case Requirements.NETWORK_TYPE_ANY:
      networkType = JobInfo.NETWORK_TYPE_ANY;
      break;
    case Requirements.NETWORK_TYPE_UNMETERED:
      networkType = JobInfo.NETWORK_TYPE_UNMETERED;
      break;
    case Requirements.NETWORK_TYPE_NOT_ROAMING:
      if (Util.SDK_INT >= 24) {
        networkType = JobInfo.NETWORK_TYPE_NOT_ROAMING;
      } else {
        throw new UnsupportedOperationException();
      }
      break;
    case Requirements.NETWORK_TYPE_METERED:
      if (Util.SDK_INT >= 26) {
        networkType = JobInfo.NETWORK_TYPE_METERED;
      } else {
        throw new UnsupportedOperationException();
      }
      break;
    default:
      throw new UnsupportedOperationException();
  }

  builder.setRequiredNetworkType(networkType);
  builder.setRequiresDeviceIdle(requirements.isIdleRequired());
  builder.setRequiresCharging(requirements.isChargingRequired());
  builder.setPersisted(true);

  PersistableBundle extras = new PersistableBundle();
  extras.putString(KEY_SERVICE_ACTION, serviceAction);
  extras.putString(KEY_SERVICE_PACKAGE, servicePackage);
  extras.putInt(KEY_REQUIREMENTS, requirements.getRequirementsData());
  builder.setExtras(extras);

  return builder.build();
}