以下代码在Nougat设备上运行正常,但在oreo设备上,它在定义的时间间隔后调用onStartJob()
,但不执行onLocationChanged
。
Job Schedular的工作部分是在奥利奥设备中完成的,但是有些onLocationChanged
从未被调用过。
创建工作分配器
用户授予位置权限后,我将执行以下代码。
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job job = createJob(dispatcher);
dispatcher.mustSchedule(job);
职位生成器
Job job = dispatcher.newJobBuilder()
.setLifetime(Lifetime.FOREVER)
.setService(LocationJobService.class)
.setTag("Location-Job-package")
.setReplaceCurrent(false)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(200, 800))
.setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
.setConstraints(Constraint.ON_ANY_NETWORK, Constraint.DEVICE_CHARGING)
.build();
return job;
后勤服务类
public class LocationJobService extends JobService implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
@Override
public void onCreate() {
super.onCreate();
if (isGooglePlayServicesAvailable()) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@SuppressLint("MissingPermission")
@Override
public boolean onStartJob(JobParameters params) {
Log.i(TAG, "onStartCommand: ");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(BACKGROUND_INTERVAL);
mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
this.params = params;
return true;
}
@Override
public void onLocationChanged(Location location) {
String storedLat, storedLng;
//....
}