Android JobScheduler无法在模拟器上启动

Kje*_*ide 9 android android-emulator

在Android模拟器上运行时,我无法让JobScheduler启动预定作业.如果我在真正的三星Galaxy S7和Nexus 5X上进行测试,两者都运行Android 6.0.1,那么预定的作业会立即开始.仿真器配置为具有100%电池和充电以及启用的全网速.

在OSX El capitan 10.11.4上运行Android Studio 2.1.1,jre 1.8.0_92-b14.仿真器版本是25.1.6,它运行的是Android 6.0.

这就是我安排工作的方式:

public static void sendInspectionSessions(Context context) {
    JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
    JobInfo job = new JobInfo.Builder(
            INSPECTION_SESSION_JOB_ID,
            new ComponentName(context, InspectionSessionJobService.class))
            .setBackoffCriteria(TimeUnit.SECONDS.toMillis(30), JobInfo.BACKOFF_POLICY_EXPONENTIAL)
            .setPersisted(true)
            .setMinimumLatency(0)
            .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
            .setOverrideDeadline(Config.INSPECTION_SESSION_SEND_DEADLINE)
            .build();
    int id = js.schedule(job);
    if (id > 0) {
        LogUtil.i(TAG, "Scheduled InspectionSession send job with id " + id + ". Supplied id was " + INSPECTION_SESSION_JOB_ID);
    } else {
        LogUtil.e(TAG, "Failed to schedule InspectionSession send job. Return code was " + id);
    }
}
Run Code Online (Sandbox Code Playgroud)

schedule()在模拟器和设备上都返回1.模拟器上未显示任何错误消息.

我通过以下方式在AndroidManifest中定义了我的工作服务:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".service.InspectionSessionJobService"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:exported="true" />
Run Code Online (Sandbox Code Playgroud)

我错过了什么,或者这是Android模拟器的错误?有没有人让JobScheduler在模拟器上工作?

小智 0

可能为时已晚,但是您在哪里调用 sendInspectionSessions() 呢?您必须将应用程序上下文传递给它。

对于其他人:您可以在应用程序的 onCreate() 方法中启动它