1

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

那么这里有什么问题呢?

4

1 回答 1

1

根据需求在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 类中

/**
 * 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;
    }

在片段/活动中

@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);
    }
于 2019-05-16T07:53:55.410 回答