【发布时间】:2018-05-28 09:05:36
【问题描述】:
这就是我启动定位服务的方式。
private void initLocationService() {
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
if(getPermissions(android.Manifest.permission.ACCESS_FINE_LOCATION,GET_LOCATION_PERMISSION)){
startService(new Intent(HomeActivity.this, LocationUpdateService.class));
}
}
在LocationUpdateService 中,我将其作为STICKY 服务启动。所以它一直在后台工作。
public class LocationUpdateService extends Service implements
LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
IServiceResponse {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand: ");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(BACKGROUND_INTERVAL);
mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
由于我的应用目前针对 Api 25(Nougat),我需要在此特定服务中进行哪些更改才能使其在 Oreo 中工作
在使用compileSdkVersion 25 时,我仍然无法访问startForegroundService() 方法。
这些是我的gradle 设置。
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
minSdkVersion 21
targetSdkVersion 25
}
【问题讨论】:
-
检查这个链接你需要为奥利奥创建一个频道,因为服务在奥利奥的后台不工作。 codelabs.developers.google.com/codelabs/…
标签: android performance android-location android-8.0-oreo