LocationServices.SettingsApi使用RESULT_CANCELED调用onActivityResult?

时间:2015-10-12 13:22:32

标签: android android-activity gps onactivityresult

在一个特定情况下,我一直在努力使用新的LocationServices.SettingsApi。 我已经在SplashActivity for Location设置上编写代码(Google GPS对话框自动打开GPS),它在Lollipop上运行完美,但在像Kitkat或Jelly Bean这样的低版本中,它没有显示google对话框,它在onActivityResult中显示RESULT_CANCELED .. :( < / p>

PFA 谷歌对话框图像 代码段Google Dialog Box for automatically turning GPS on

if(googleApiClient == null) {

        googleApiClient = new GoogleApiClient.Builder(MapsActivity.this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this).build();
        googleApiClient.connect();

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(30 * 1000);
        locationRequest.setFastestInterval(5 * 1000);
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);

        //**************************
        builder.setAlwaysShow(true); //this is the key ingredient
        //**************************

        PendingResult<LocationSettingsResult> result =
                LocationServices.SettingsApi.checkLocationSettings(googleApiClient, 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.
                        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(
                                   MapsActivity.this, 1000);
                        } 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;
                }
            }
        });             }

以上代码用于自动转换gps

以下代码是onActivityResult

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    LatLng currentLoc = LocationProvider.currentLoc;

    switch (resultCode) {
        case Activity.RESULT_OK:
            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            if(mLastLocation!=null)
            {
                currentLoc = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
            }

            Log.e(TAG, "RESULT OK");
            break;
        case Activity.RESULT_CANCELED:
            checkGPSstatus();
            Log.e(TAG, "RESULT_CANCELED");
            break;
        default:
            break;
    }

2 个答案:

答案 0 :(得分:3)

让我们试试猜。您的活动是否launchMode设置为singleInstance?如果是这样,您不允许其他活动成为您的任务的一部分。

答案 1 :(得分:0)

请在Github上重新审视此项目:

https://github.com/googlesamples/android-play-location

您可以尝试按照其中一个示例重新编写代码,因为它适用于KitKatJellyBean以及Lollipop

我已经在几天前实施了它,并且正在以这种方式工作。