Oreo 未调用定位服务
Location service not getting called from Oreo
我开始 LocationService
使用 startService()
命令,但由于我了解到当应用程序处于后台时奥利奥不会接受此服务,我切换到 JobSchedular
,
我在 lollipop
中测试了该应用程序,JobSchedular
工作正常,但在 Oreo 中,它没有 运行 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);
}
位置更新服务
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
}
此问题与以下代码有关:
JobInfo.Builder builder= new JobInfo.Builder(11, componentName);
builder.setPeriodic(5000);
从 AndroidN 开始,JobScheduler 以最小 15 分钟的周期工作。 5秒的频率太频繁了,不合适。
我开始 LocationService
使用 startService()
命令,但由于我了解到当应用程序处于后台时奥利奥不会接受此服务,我切换到 JobSchedular
,
我在 lollipop
中测试了该应用程序,JobSchedular
工作正常,但在 Oreo 中,它没有 运行 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);
}
位置更新服务
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
}
此问题与以下代码有关:
JobInfo.Builder builder= new JobInfo.Builder(11, componentName);
builder.setPeriodic(5000);
从 AndroidN 开始,JobScheduler 以最小 15 分钟的周期工作。 5秒的频率太频繁了,不合适。