【问题标题】:Can not get GPS current location when open the app with off GPS在关闭 GPS 的情况下打开应用程序时无法获取 GPS 当前位置
【发布时间】:2019-01-05 09:57:44
【问题描述】:

我有一个使用谷歌地图当前位置获取用户位置的网络视图 android 应用程序,当用户单击一个图标时,我正在调用本机函数来访问 gps,问题是当使用离线 GPS 打开应用程序时,然后点击图标,它会在 GPS 上询问,当按下 ok 时什么都不会发生,即使再次点击图标仍然不会发生任何事情,除非关闭应用程序并再次打开它才会起作用,请帮助它真的让我发疯了.. .

public void location_request()

{
    final 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);
    GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
    mGoogleApiClient.connect();
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, 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:
                   //The client can initialize location
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied.
                    try {

                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);

                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    break;
            }
        }
    });
}

【问题讨论】:

  • 您是否添加了适当的权限?
  • 是的,它没有权限问题,因为如果 GPS 处于开启状态或刷新页面,那么它将起作用..

标签: java android google-maps gps


【解决方案1】:

使用此代码启用 GPS 并确保已授予位置权限。最初,当 GPS 被禁用时,onFailureListener 将触发并弹出 GPS 启用对话框,但在按下确定后它不会触发 onSuccessListener,因此您需要从 onActivityResult 发出当前位置请求,但如果在进入应用程序之前启用 GPS,则 onSuccessListener 将触发并且您可以请求从那里获取当前位置。

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

            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(mLocationRequest);

            SettingsClient client = LocationServices.getSettingsClient(HomeActivity.this);
            Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

            task.addOnSuccessListener(HomeActivity.this, locationSettingsResponse -> {
                // Make your current location request here

            });

            task.addOnFailureListener(HomeActivity.this, e -> {
                if (e instanceof ResolvableApiException) {
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(HomeActivity.this,
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                    }
                }
            });


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == REQUEST_CHECK_SETTINGS) {
            // Make your current location request here
        }
    }

参考:https://developer.android.com/training/location/change-location-settings

【讨论】:

  • 替换您的代码而不是我的代码时出现此错误:此语言级别不支持 Lambda 表达式
  • 我搜索并找到了先前错误的解决方案,但 @Override 位置错误,它给出了此错误:此处不允许注释
  • 您是否在 location_request() 方法中覆盖 onActivityResult 方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多