Google 位置设置未按预期工作

Posted

技术标签:

【中文标题】Google 位置设置未按预期工作【英文标题】:Google location settings not working as exptected 【发布时间】:2017-05-21 07:58:41 【问题描述】:

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

这是我的代码:

`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】:

使用此功能可实现高精度定位

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;

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

【讨论】:

以上是关于Google 位置设置未按预期工作的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 传递依赖排除未按预期工作。 (我如何摆脱 com.google.guava:guava-jdk5:13.0 ?)

动态脚本标签加载未按预期工作

Oauth2 安全配置匹配器请求过滤未按预期工作

用于鼠标位置的 Python Kalman 滤波器未按预期工作

对象数组未按预期运行

通过 devenv.com 构建设置项目未按预期工作