android - 在不显示对话的情况下设置 GPS

标签 android

深入搜索后,我可以在应用程序恢复时使用对话框打开 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/

相关文章:

android - 回收站 View 未显示

android - android模拟器上的内存不足错误,但在设备上没有

java - 如何在android中构建CLM

android - ContentObserver 不适用于 Nexus 7 上的浏览​​器书签?

android - 无法解析符号 SmsManager.getDefault()

java - Google Maps Android API 切换楼层时删除标记

android - 有什么方法可以将一些字符串添加到 Android Studio 中正则表达式找到的内容中吗?(ctrl+shift+R)

java - Android Studio下面的类无法实例化?

android - 媒体记录器 : stop called in an invalid state: 1

android - 如何使用 Kotlin 更改 android 中的字体?