打开GPS设置不起作用

时间:2016-04-05 07:13:16

标签: android android-intent gps latitude-longitude

在我的应用程序中,我想打开gps设置页面,如果没有启用它。为了在谷歌搜索时这样做,大多数人都说这段代码应该有用;

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

该代码打开此页面,

enter image description here

正如您在红色框中看到的,位置设置为ON。但是当我获得纬度和经度值时,它们返回null。然后我意识到我应该点击绿色框中的GPS按钮来启用GPS设置。但是我怎么能以编程方式呢?这意味着startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));无法打开实际的GPS设置。是因为我使用模拟器吗?见下文,我的GPS实际上没有启用。

enter image description here

有什么想法吗?提前致谢

1 个答案:

答案 0 :(得分:0)

只有在您想要位置设置页面时,您的代码才有效。现在有办法在不询问用户的情况下以编程方式打开GPS。我的一个工作中也遇到了这个问题,我找到了一个使用LocationServices.SettingsApi.checkLocationSettings的解决方案。现在我将尝试向您展示如何逐步使用它

  1. 假设您有一个按钮,当您点击此按钮时,您想要显示位置弹出窗口以启用GPS

    enableGPS.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showLocationEnableDialog();
        }
    });
    
  2. 点击此按钮后,您应检查GoogleApiClient是否为空,如果为空,则应在showLocationEnableDialog()

    创建实例
    public void showLocationEnableDialog() {
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(getActivity())
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();
    
        createLocationRequest();
        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
        result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
            @Override
            public void onResult(LocationSettingsResult result) {
                setCallback(result);
            }
        });
    }
    

    }

  3. 您应该定义createLocationRequest()setCallback()方法

    private void createLocationRequest() {
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(10 * 1000);
        locationRequest.setFastestInterval(5 * 1000);
        builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);
        builder.setAlwaysShow(true); //this is the key ingredient
    }
    
    private void setCallback(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.
            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(getActivity(), MainActivity.REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException e) {
                // 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;
        }
    }
    
  4. 最后一步是抓住我们活动status.startResolutionForResult(getActivity(), MainActivity.REQUEST_CHECK_SETTINGS);上的onActivityResult。重要的是,我在片段中使用了这些代码,因此如果您在活动中使用此代码,则应将getActivity()替换为您的活动的上下文。此外REQUEST_CHECK_SETTINGS等于public static final int REQUEST_CHECK_SETTINGS = 0x1;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        switch (requestCode) {
        // Check for the integer request code originally supplied to startResolutionForResult().
            case REQUEST_CHECK_SETTINGS: //int REQUEST_CHECK_SETTINGS = 0x1;
               switch (resultCode) {
                   case Activity.RESULT_OK:
                   //user enabled GPS, you can do your GPS operation here again
                   break;
                   case Activity.RESULT_CANCELED:
                   // On pop-up user clicked cancel
                   break;
               }
            break;
            }
    }