Scheduled JobService 仅在我打开我的应用程序时运行

Scheduled JobService runs only when I open my app

我使用 JobScheduler 创建了一个必须每 15 分钟 运行 定期执行的作业。但它只有在我的应用程序打开时才有效。我

当我 运行 一个接一个地应用所有排队的作业 运行 时,即使在设备重新启动后也是如此。

AndroidManifest.xml

<service
            android:name=".MyJobService"
            android:permission="android.permission.BIND_JOB_SERVICE" />

MyJobService.class

public class MyJobService extends JobService {
    private static final String TAG = "MyJobService";

    @Override
    public boolean onStartJob(JobParameters params) {
        Log.d(TAG, "onStartJob: job started");
        return false;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d(TAG, "onStopJob: job stopped");
        return false;
    }
}

这就是我设置 JobScheduler 的方式

public static void scheduleJob(Context context) {
    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
    ComponentName componentName = new ComponentName(context, MyJobService.class);

    JobInfo.Builder builder = new JobInfo.Builder(1337, componentName);
    builder.setPeriodic(15 * 60 * 1000);
    builder.setPersisted(true);
    builder.build();

    if (jobScheduler.schedule(builder.build()) <= 0) {
        Logr.d(TAG, "scheduleJob: Some error while scheduling the job");
    }
}

所以这里有什么问题?

根据需要在activity/fragment onCreate()/onResume 中调用此方法。

public static void scheduleJob(Context context) {
    JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
    ComponentName componentName = new ComponentName(context, MyJobService.class);

    JobInfo.Builder builder = new JobInfo.Builder(1337, componentName);
    builder.setPeriodic(15 * 60 * 1000);
    builder.setPersisted(true);
    builder.build();

    if (jobScheduler.schedule(builder.build()) <= 0) {
        Logr.d(TAG, "scheduleJob: Some error while scheduling the job");
    }
}

在 MyJobService 中 Class

/**
 * When the app's activity/fragment is created, it starts this service. This is so that the
 * activity and this service can communicate back and forth. See "setUiCallback()"
 */
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "onStartCommand");
    return START_NOT_STICKY;
}
 @Override
    public boolean onStartJob(JobParameters params) {
        Log.i(TAG, "onStartJob" + mConnectivityReceiver);
        registerReceiver(mConnectivityReceiver, new IntentFilter(Constants.CONNECTIVITY_ACTION));
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        Log.i(TAG, "onStopJob");
        unregisterReceiver(mConnectivityReceiver);
        return true;
    }

在fragment/activity

@Override
    protected void onStop() {
        // A service can be "started" and/or "bound". In this case, it's "started" by this Activity
        // and "bound" to the JobScheduler (also called "Scheduled" by the JobScheduler). This call
        // to stopService() won't prevent scheduled jobs to be processed. However, failing
        // to call stopService() would keep it alive indefinitely.
        stopService(new Intent(this, NetworkSchedulerService.class));
        super.onStop();
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Start service and provide it a way to communicate with this class.
        Intent startServiceIntent = new Intent(this, NetworkSchedulerService.class);
        startService(startServiceIntent);
    }