开启gps而不显示对话

时间:2018-11-02 09:33:36

标签: android

深入搜索后,我可以在应用恢复时使用对话框在gps上运行。我使用了在Google上找到的以下代码。

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上不显示此对话框。如何解决此问题?

1 个答案:

答案 0 :(得分:2)

  

但是我想在gps上不显示此对话框

没有可能的合法方法,因为那将是主要的隐私流程。也许您可以在有根电话上进行操作(尽管我不确定,但是有时我会在某个论坛上阅读有关此问题的文章。)但是android不允许在没有用户干预的情况下打开GPS。

您可以为用户提供进入设置菜单的选项,从那里他们可以启用GPS(以旧式方式),也可以使用GoogleApiClient来启用单次点击(如Google地图)的GPS。