Google位置设置无法正常运行

时间:2017-05-21 07:58:42

标签: java android android-studio localization geolocation

我正在使用谷歌功能启用和禁用用户的位置,我的主要目标是,我输入相机活动,它询问用户是否点击取消它回到上一个活动如果一切正常它让用户拍照。

这是我的代码:

`public class GoogleLocation extends Activity {
private static final int REQUEST_CHECK_SETTINGS = 5 ;

public static void displayLocationSettingsRequest(final Context context) {

    GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API).build();
    googleApiClient.connect();

    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(10000 / 2);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
    builder.setAlwaysShow(true);


    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();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.i("location", "All location settings are satisfied.");
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.i("location", "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");
                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
                        Log.d("HELLO2","HELLO2");
                        status.startResolutionForResult((Activity) context, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        Log.i("location", "PendingIntent unable to execute request.");
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Log.i("location", "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                    break;
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.d("requestCode",String.valueOf(requestCode));
    Log.d("resultCode",String.valueOf(resultCode));
    if (requestCode == REQUEST_CHECK_SETTINGS) {
        Log.d("ENTERED","ENTERED");
        if(resultCode == RESULT_OK){

        }
        if(resultCode == RESULT_CANCELED){
            Log.d("ENTERED","ENTERED");
            this.finish();
        }
    }
}

} ` 我认为我可以通过onactivityresult来控制对话框选项'ok'和'cancel',但是正如你们可以在onactivity结果中看到的那样我有2个Log.d这个当我从相机中预先设置'x'时会被解雇关闭相机,发生了什么?我需要控制对话框,但它不起作用:/。

任何提示?

由于

1 个答案:

答案 0 :(得分:0)

使用此功能以高精度启用位置

public static void checkLocationSettings(final Activity context){

    if(!LocationUtil.isLocationServicesAvailable(context)){

        GoogleApiClient googleApiClient = new GoogleApiClient.Builder(context)
                .addApi(LocationServices.API).build();
        googleApiClient.connect();

        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        locationRequest.setInterval(10000);
        locationRequest.setFastestInterval(10000 / 2);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(locationRequest);
        builder.setAlwaysShow(true);

        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();
                switch (status.getStatusCode()) {
                    case LocationSettingsStatusCodes.SUCCESS:
                        Log.e(TAG, "All location settings are satisfied.");
                        break;
                    case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                        Log.e(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                        try {
                            // Show the dialog by calling startResolutionForResult(), and check the result in onActivityResult().
                            status.startResolutionForResult(context, REQUEST_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException e) {
                            Log.e(TAG, "PendingIntent unable to execute request.");
                        }
                        break;
                    case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                        Log.e(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                        break;
                }
            }
        });
    }

使用此功能检查位置是否以高精度启用

public static boolean isLocationServicesAvailable(Context context) {
    int locationMode = 0;
    boolean isAvailable = false;

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT){
        try {
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }

        isAvailable = (locationMode != Settings.Secure.LOCATION_MODE_OFF && locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
    }
    return isAvailable;
}

如果用户点击确定,则该位置将以高精度启用。否则它将保持当前状态。如果位置已经启用且准确度不高,那么它也会提示将位置模式更改为高精度。