【发布时间】:2016-02-26 10:57:13
【问题描述】:
我正在创建一个需要激活位置服务的 android 应用。有什么办法可以强制在android中启用位置服务。我不希望用户进入设置页面并启用它。
PS:该应用不适合公众使用,如果需要,可以为该应用提供 root 访问权限。
【问题讨论】:
标签: android android-location android-settings
我正在创建一个需要激活位置服务的 android 应用。有什么办法可以强制在android中启用位置服务。我不希望用户进入设置页面并启用它。
PS:该应用不适合公众使用,如果需要,可以为该应用提供 root 访问权限。
【问题讨论】:
标签: android android-location android-settings
试试这个:
public static void locationChecker(GoogleApiClient googleApiClient, final Activity activity) {
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
@Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
// All location settings are satisfied. The client can initialize location
// requests here.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied. But could be fixed by showing the user
// a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
status.startResolutionForResult(
activity, 1000);
} catch (IntentSender.SendIntentException e) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way to fix the
// settings so we won't show the dialog.
break;
}
}
}
);
}
在您的 Activity 中,将以下代码放入 onActivityResult():
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000) {
if (resultCode == Activity.RESULT_CANCELED) {
finish();
} else {
// this should not be needed, but apparently is in 8.1
//user granted the permission
}
}
}
【讨论】: