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

时间:2019-05-16 07:43:53

标签: android job-scheduling jobservice

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

那这里怎么了?

1 个答案:

答案 0 :(得分:0)

根据需要在活动/片段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);
    }