首页 > 解决方案 > 在不显示对话的情况下打开 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。我该如何解决这个问题?

标签: android

解决方案


但我想在不显示此对话框的情况下使用 gps

没有可能的合法方式来做到这一点,因为这将是一个主要的隐私流程。也许在有根手机上你可能可以做到,(虽然我不确定,但有时我在某个论坛上读到了一些关于这个的文章。)但是android不允许在没有用户干预的情况下打开GPS。

您可以让用户选择进入设置菜单,从那里他们可以启用 GPS(旧学校方式),或者使用GoogleApiClient像谷歌地图一样单击来启用 GPS。


推荐阅读