【发布时间】:2018-05-30 08:11:16
【问题描述】:
我是用startService() 命令启动LocationService,但由于我知道当应用程序处于后台时奥利奥不会提供此服务,所以我切换到JobSchedular,
我在lollipop 和JobSchedular 中测试了该应用程序运行良好,但在奥利奥中,它无法运行LocationService
我将break point 放在LocationService 的onCreate() 方法中,它就是不去那里。
这就是我正在做的。
MainActivity
它执行以下代码,但不响应LocationUpdateService.class
public void initLocationJob(){
JobInfo jobInfo;
JobScheduler jobScheduler;
ComponentName componentName= new ComponentName(this, LocationUpdateService.class);
JobInfo.Builder builder= new JobInfo.Builder(11, componentName);
builder.setPeriodic(5000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPersisted(true);
jobInfo= builder.build();
jobScheduler= (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(jobInfo);
}
LocationUpdateService
public class LocationUpdateService extends JobService implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
IServiceResponse {
@Override
public void onCreate() {
super.onCreate();
if (isGooglePlayServicesAvailable()) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mGoogleApiClient.connect();
}
}
@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 boolean onStopJob(JobParameters params) {
Log.d("JobStopped", "JobStopped");
return true;
}
@Override
public void onLocationChanged(Location location) {
//Get current Lat/lng and send it to server
}
【问题讨论】:
标签: android performance android-location android-8.0-oreo android-jobscheduler