下面列出了android.app.job.JobInfo#NETWORK_TYPE_UNMETERED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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");
}
}
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();
}
}
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();
}
}
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();
}
}
@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();
}
@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();
}