android - 在不显示对话的情况下打开 GPS
问题描述
深入搜索后,当应用程序恢复时,我可以使用对话框打开 gps。我使用了在谷歌上找到的以下代码。
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);//Setting priotity of Location request to high
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);//5 sec Time interval for location update
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true); //this is the key ingredient to show dialog always when GPS is off
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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.
updateGPSStatus("GPS is Enabled in your device");
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(MainActivity.this, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
// 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;
}
}
});
此代码工作正常。但我想在不显示此对话框的情况下使用 gps。我该如何解决这个问题?
解决方案
但我想在不显示此对话框的情况下使用 gps
没有可能的合法方式来做到这一点,因为这将是一个主要的隐私流程。也许在有根手机上你可能可以做到,(虽然我不确定,但有时我在某个论坛上读到了一些关于这个的文章。)但是android不允许在没有用户干预的情况下打开GPS。
您可以让用户选择进入设置菜单,从那里他们可以启用 GPS(旧学校方式),或者使用GoogleApiClient
像谷歌地图一样单击来启用 GPS。
推荐阅读
- angularjs - attempting to navigate a user to login if cookie is undefined
- javascript - Would like to understand why this "setInterval" in my code isnt working?
- android - 从同一个项目安装多个版本的 apk
- c# - 从 sqlite 数据库下载 byte[] 并转换为照片
- sql - Powershell读取包含“加号”的sql脚本
- jenkins - 具有多个部署的单个 Jenkins 作业
- javascript - How can I Interleave / merge async iterables?
- c++ - 防止 clang++ 使用默认包含
- node.js - 如何在我的项目中使用自定义版本的 npm 包?
- reactjs - 来自 react-transition-group 的 CSSTransition 不退出