深入搜索后,我可以在应用程序恢复时使用对话框打开 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。我该如何解决这个问题?
最佳答案
But i want to on gps without showing this dialogue box
没有合法的方法可以做到这一点,因为这将是一个主要的隐私流程。也许在 root 手机上你可以做到这一点,(虽然我不确定,但有时我在某个论坛上阅读了一些关于这个的文章。)但是 android 不允许在没有用户干预的情况下打开 GPS。
您可以让用户选择从他们可以启用 GPS 的位置转到设置菜单(老派方式),或者使用 GoogleApiClient
像谷歌地图一样通过单击启用 GPS。
关于android - 在不显示对话的情况下设置 GPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53115951/