JobScheduler 在条件满足时不会重启

JobScheduler won't restart when condition meets the requirements

我已将 JobScheduler 设置为在 phone 充电并连接到 wifi 时启动。但是当我关闭 wifi 时它说作业已取消,但是当我重新打开它时它不会重新启动。这是我的代码:

MainActivity.java

private static final String TAG="MainActivity";
public void scheduleJob(View v) {
    ComponentName componentName = new ComponentName(this, ExampleJobService.class);
    JobInfo info = new JobInfo.Builder(123, componentName)
            .setRequiresCharging(true)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
            .setPersisted(true)
            .setPeriodic(15 * 60 * 1000)
            .build();

    JobScheduler scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
    int resultCode = scheduler.schedule(info);
    if (resultCode == JobScheduler.RESULT_SUCCESS) {
        Log.d(TAG, "Job scheduled");
    } else {
        Log.d(TAG, "Job scheduling failed");
    }
}

public void cancelJob(View v){
    JobScheduler scheduler=(JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
    scheduler.cancel(123);
    Log.d(TAG,"Job cancelled");
}


ExampleJobService.java

private static final String TAG="ExampleJobService";
private boolean jobCanceled=false;

@Override
public boolean onStartJob(JobParameters params) {
    Log.d(TAG, "Job started");
    doBackgroundWork(params);

    return true;
}

private void doBackgroundWork(final JobParameters params) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                Log.d(TAG, "run: " + i);
                if (jobCanceled) {
                    return;
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Log.d(TAG, "Job finished");
            jobFinished(params, false);
        }
    }).start();
}

@Override
public boolean onStopJob(JobParameters params) {
    Log.d(TAG, "Job cancelled before completion");
    jobCanceled = true;
    return true;
}

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hoversfw.test">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".ExampleJobService"
            android:permission="android.permission.BIND_JOB_SERVICE"/>
    </application>

</manifest>

当我关闭 wifi、安排作业、终止应用程序进程并打开 wifi 时,JobScheduler 启动,我看到它在 Logcat 中打印。此外,当我安排它并启动时,然后终止应用程序,它只是在 Logcat 中停止打印。所以我认为只要 JobScheduler 被打断,它就不会重新启动。我加了权限,minSDK是21的,所以权限应该没有问题。帮助

JobSchleduler 将在您调用 jobFinished(params, true) 时重新启动。可以在return

之前加上
if (jobCanceled) {
    jobFinished(params, true);
    return;
}