我正在尝试实现一个jobscheduler来调用jobservice,它每5分钟触发一次以捕获在特定地理位置上花费的时间。但下面的代码适用于棉花糖及以上,但它使 Lollipop 及以下的崩溃。
这是我的代码->

ComponentName serviceComponent = new ComponentName(context, SyncDbToServerJobService.class);
        JobInfo.Builder builder = new JobInfo.Builder(0, serviceComponent);
        PersistableBundle bundle = new PersistableBundle();
        bundle.putString(Constants.ENTERED_TIME, enteredTime);
        builder.setPeriodic(intervalMillis).setExtras(bundle).setPersisted(true);
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED); // require unmetered network
        builder.setRequiresDeviceIdle(true); // device should be idle
        builder.setRequiresCharging(false); // we don't care if the device is charging or not
        JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
        jobScheduler.schedule(builder.build());

这就像犯了一个错误->
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);

这是我的日志错误->
04-19 11:38:25.358 1986-4658/com.eonelectric.eon E/AndroidRuntime: FATAL EXCEPTION: IntentService[GeofenceTransitionsIS]
                                                               Process: com.eonelectric.eon, PID: 1986
                                                               java.lang.NoSuchMethodError: No virtual method getSystemService(Ljava/lang/Class;)Ljava/lang/Object; in class Landroid/content/Context; or its super classes (declaration of 'android.content.Context' appears in /system/framework/framework.jar)
                                                                   at com.eonelectric.eon.GeneralUtil.startSyncAlarm(GeneralUtil.java:440)
                                                                   at com.eonelectric.eon.beatTrack.GeofenceTransitionsIntentService.getGeofenceTransitionDetails(GeofenceTransitionsIntentService.java:137)
                                                                   at com.eonelectric.eon.beatTrack.GeofenceTransitionsIntentService.onHandleIntent(GeofenceTransitionsIntentService.java:96)
                                                                   at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                   at android.os.Looper.loop(Looper.java:135)
                                                                   at android.os.HandlerThread.run(HandlerThread.java:61)

最佳答案

试试这个它适用于 Lollipop

ComponentName jobService =
                new ComponentName(getApplicationContext().getPackageName(), SyncService.class.getName());
JobInfo.Builder builder = new JobInfo.Builder(0, jobService);
builder.setPeriodic(10000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
jobScheduler = (JobScheduler) this.getSystemService(JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());

关于android-jobscheduler - java.lang.NoSuchMethodError : No virtual method getSystemService,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43487868/

10-13 03:13