JobScheduler-NETWORK_TYPE_NONE 给出 IllegalArgumentException

JobScheduler-NETWORK_TYPE_NONE gives IllegalArgumentException

我正在使用以下代码安排工作服务。

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

但低于异常

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.

但是当我将 setRequiredNetworkType(NETWORK_TYPE_NONE) 更改为 setRequiredNetworkType(NETWORK_TYPE_ANY) 时,它起作用了 fine.But 我希望我的工作服务 运行 即使没有网络 connection.Why 我NETWORK_TYPE_NONE 出现异常?

你必须有某种约束,否则它总是会抛出 IllegalArgumentException,设置任何约束或者只使用 AlarmManager 或 WorkManager 查看代码段,这是来自 Android 源代码

        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.");
        }

我遇到了同样的问题,只是改用了 AlarmManager