Android 打开设备';不询问用户的位置访问选项
我正在为Android KitKat版本(最低API级别19)开发一个Android应用程序,其中我需要在不询问用户的情况下自动打开用户设备的位置访问选项。我已经尝试过这段代码(如下所示),但它会弹出一个对话框并询问用户。所以,请告诉我如何在不询问用户的情况下打开设备的位置访问 代码:Android 打开设备';不询问用户的位置访问选项,android,Android,我正在为Android KitKat版本(最低API级别19)开发一个Android应用程序,其中我需要在不询问用户的情况下自动打开用户设备的位置访问选项。我已经尝试过这段代码(如下所示),但它会弹出一个对话框并询问用户。所以,请告诉我如何在不询问用户的情况下打开设备的位置访问 代码: public static void enableDeviceLocation(final Activity activity) { final GoogleApiClient googleApiCli
public static void enableDeviceLocation(final Activity activity) {
final GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity.getApplicationContext()).addApi(LocationServices.API).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
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) {
Status status = result.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
//All location settings are satisfied.
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
//Location settings are not satisfied. Show the user a dialog to upgrade location settings
try {
// Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
status.startResolutionForResult(activity, LocationRequest.PRIORITY_HIGH_ACCURACY);
}
catch (IntentSender.SendIntentException e) {
//PendingIntent unable to execute request.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//Location settings are inadequate, and cannot be fixed here. Dialog not created.
break;
}
googleApiClient.disconnect();
}
});
}
公共静态无效启用设备位置(最终活动){
final GoogleApiClient GoogleApiClient=new GoogleApiClient.Builder(activity.getApplicationContext()).addApi(LocationServices.API.build();
googleApiClient.connect();
LocationRequest LocationRequest=LocationRequest.create();
locationRequest.setPriority(locationRequest.PRIORITY\u高精度);
LocationSettingsRequest.Builder=新建LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult=LocationServices.SettingsApi.checkLocationSettings(GoogleAppClient,builder.build());
result.setResultCallback(新的ResultCallback(){
@凌驾
公共void onResult(位置设置结果){
Status Status=result.getStatus();
开关(status.getStatusCode()){
案例位置设置StatusCodes.SUCCESS:
//满足所有位置设置。
打破
案例位置设置StatusCodes.RESOLUTION_要求:
//不满足位置设置。向用户显示一个对话框以升级位置设置
试一试{
//通过调用startResolutionForResult()显示对话框,并在onActivityResult()中检查结果。
状态。StartResult解决方案(活动、位置请求、优先级和高精度);
}
catch(IntentSender.sendtintentexe){
//PendingEvent无法执行请求。
}
打破
案例位置设置StatusCodes.SETTINGS\u CHANGE\u不可用:
//位置设置不足,无法在此修复。未创建对话框。
打破
}
googleApiClient.disconnect();
}
});
}
在标准无根安卓设备上,出于明显的隐私和安全原因,您无法在没有用户参与的情况下自动启用任何位置提供商。在标准无根安卓设备上,您无法在没有用户参与的情况下自动启用任何位置提供商,出于明显的隐私和安全原因。简单地说,出于安全考虑,你不能这样做。否则,您可能会跟踪未被注意到的用户。这就是为什么在更现代的版本上,你甚至必须在运行时询问。简单地说,为了安全,你不能这样做。否则,您可能会跟踪未被注意到的用户。这就是为什么在更现代的版本中,您甚至必须在运行时询问。