如果我的应用程序针对牛轧糖设备,startService() 是否仍然有效
Will startService() still work if my app targeting nougat devices
这就是我启动定位服务的方式。
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
}
startForegroundService() 可从 API 级别 26 NOT 来自 API level 25
.
把compileSdkVersion 25
和buildToolsVersion "25.0.3"
改成最新的试试
例如,在我的例子中,我使用以下:
compileSdkVersion 26
buildToolsVersion '27.0.3'
这就是我启动定位服务的方式。
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
}
startForegroundService() 可从 API 级别 26 NOT 来自 API level 25
.
把compileSdkVersion 25
和buildToolsVersion "25.0.3"
改成最新的试试
例如,在我的例子中,我使用以下:
compileSdkVersion 26
buildToolsVersion '27.0.3'