I am using below code to schedule a job service.
JobScheduler jobScheduler = (JobScheduler) mContext.getApplicationContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (jobScheduler != null) {
try {
jobScheduler.schedule(AppJobService.createJobInfo(mContext.getApplicationContext(), account));
} catch (IllegalArgumentException e) {
CrashLogger.logException(e);
}
}
public static JobInfo createJobInfo(@NonNull Context context, Account account) {
Gson g = new Gson();
String json = g.toJson(account);
PersistableBundle bundle = new PersistableBundle();
bundle.putString("Account", json);
JobInfo.Builder builder = new JobInfo.Builder(3, new ComponentName(context, AppJobService.class))
.setExtras(bundle)
.setRequiredNetworkType(NETWORK_TYPE_NONE)
.setRequiresDeviceIdle(false).setPersisted(false);
return builder.build();
}
But getting below exception
2018-12-03 17:51:22.360 5032-5557/? W/System.err: java.lang.IllegalArgumentException: You're trying to build a job with no constraints, this is not allowed.
But when I change setRequiredNetworkType(NETWORK_TYPE_NONE)
to setRequiredNetworkType(NETWORK_TYPE_ANY)
it works fine.But I want my job service to run even when there is no network connection.Why I am getting exception with NETWORK_TYPE_NONE?
You must have some kind of constraint, or it will always throw an IllegalArgumentException, put any kind of constraint or just use AlarmManager or WorkManager Check out the snippet, this is from the Android source code
public JobInfo build() {
// Allow jobs with no constraints - What am I, a database?
if (!mHasEarlyConstraint && !mHasLateConstraint && mConstraintFlags == 0 &&
mNetworkRequest == null &&
mTriggerContentUris == null) {
throw new IllegalArgumentException("You're trying to build a job with no " +
"constraints, this is not allowed.");
}
I had the same problem and I just used AlarmManager instead