Google Play服务LocationSettingsAPI - LocationSettingsRequest对话框未显示

时间:2015-06-08 21:18:10

标签: android android-intent android-activity google-play-services

我想知道你们中是否有人可以帮助我。我真的可以使用你的专业知识。

目前,我已在谷歌播放服务的帮助下实施了位置跟踪功能。它能够相当准确地测量当前速度和总距离。我希望像谷歌地图一样有一键启用功能,我已经完成了Android开发者页面显示的实现。目前,我有以下代码(不是全部,但是在开启GPS之前不会调用整个'requestLocationUpdates',似乎没有必要在这里显示:

mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();

    mLocationRequest = new LocationRequest()
            .setInterval(5000)
            .setFastestInterval(500)
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    mGoogleApiClient.connect();

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    LocationSettingsRequest request = builder.build();

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, request);

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            final LocationSettingsStates locationSettingsStates = result.getLocationSettingsStates();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.

                    Log.d("EquiMoves", "Success");
                    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(
                                activity,
                                1000);


                        Log.d("EquiMoves", "Resolution required");
                    } 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.
                    Log.d("EquiMoves", "Fail");
                    break;
            }
        }
    });

onActivityResult方法:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
    switch (requestCode) {
        case 1000:
            switch (resultCode) {
                case Activity.RESULT_OK:
                    // All required changes were successfully made
                    Log.d("EquiMoves", "Ok");
                    break;
                case Activity.RESULT_CANCELED:
                    // The user was asked to change settings, but chose not to
                    Log.d("EquiMoves", "Canceled");
                    break;
                default:
                    Log.d("EquiMoves", "Default");
                    break;
            }
            break;
    }
}

正如您所看到的,代码中有一些日志。在这些日志之后,它告诉我它通过所有函数甚至到达onActivityResult,它立即进入Activity.RESULT_CANCELED(因为日志显示“已取消”。

我无法弄清楚为什么我没有得到对话,我希望你们中的任何人都可以帮助我。

提前感谢您,祝您有个美好的一天!

问候,

碧玉

0 个答案:

没有答案
相关问题