我正在尝试实现一个jobcheduler来调用jobservice,它会在每5分钟触发一次,以捕获在特定geoLocation中花费的时间。但是下面的代码适用于Marshmallow及以上版本,但它对棒棒糖及以下版本造成了冲击。 这是我的代码 - >
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)
答案 0 :(得分:2)
试试这个它适用于棒棒糖
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());
答案 1 :(得分:1)
如果您的应用针对Android 5.0(API级别21)或更高版本,Google建议您使用JobScheduler
执行后台任务,但在API级别23中添加了getSystemService(serviceClass)
。因此,请使用:
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE)
还包括API 21和22.