【问题标题】:Turn on location prompt fails when android device is in flight modeandroid设备处于飞行模式时打开位置提示失败
【发布时间】:2017-03-22 07:02:43
【问题描述】:

上下文 我有这个应用程序,它需要用户在进行任何活动之前打开位置,因为我们会在他们移动时记录位置,但是,我的一些用户选择在移动时保持飞行模式。我的所有用户都使用 android 版本 4.4 三星 Galaxy J1那是没有根的。 我们的用户位于偏远地区,没有wifi信号且网络信号较弱,因此我们仅使用gps获取位置。 下图是应用中关闭飞行模式和关闭位置时的提示。

问题:

很遗憾,在飞行模式下,并没有提示用户打开位置。使用的代码如下图来自google

public void getLocationSettings()
{
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);
    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,
                    builder.build());
    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(@NonNull LocationSettingsResult result) {
            final Status status = result.getStatus();
            //final LocationSettingsStates = 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 this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(MainActivity.this,REQUEST_CHECK_SETTINGS);
                    } 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;
            }
        }
    });
}

在飞行模式和位置​​关闭时,此检查将始终导致 SETTINGS_CHANGE_UNAVAILABLE 因此,我们将无法进行任何活动。 但是,即使用户处于飞行模式,下面的其他代码检查也只能获取位置设置。

    public static Boolean gpsStatus(Context context)
{
    LocationManager locationManager = (LocationManager) context .getSystemService(context.LOCATION_SERVICE);
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

但是,使用上面的方法,我无法执行下面的代码,提示用户通过单击对话框上的确定来打开位置:

    // 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().
  status.startResolutionForResult(MainActivity.this,REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException e) {
  // Ignore the error.
}

上面的代码提示用户单击确定以打开位置,因此用户不必离开应用程序并转到设置来打开位置。 在查看其他需要位置的应用程序时,我还注意到,由于它与谷歌地图使用的位置检查相同,在飞行模式和位置​​关闭时。谷歌地图也不会提醒用户转动位置,只会告诉他们检查他们的互联网连接。任何关于始终检查位置设置的最佳方式的想法将不胜感激,谢谢

【问题讨论】:

  • 为什么不检查用户是否开启了飞行模式并发出警报通知他将其关闭?之后,检查位置是否已打开。
  • @Adrian-AlexandruComan,飞行模式有助于节省电池电量,因为我们的用户活动整天都在离线模式下进行。他们只在晚上打开移动数据。

标签: java android location


【解决方案1】:

到目前为止,我正在使用这两种检查来处理这两种情况,即在飞行模式关闭的情况下提示用户打开位置,我们可以访问设置或只是要求用户转到设置并打开地点。 使用的代码如下所示。通过这种方法,我们能够始终成功检查位置设置并采取必要的措施。

        //go through here if gps is off and in flight mode
    if (!NativeLib.gpsStatus(MainActivity.this) && NativeLib.isAirplaneModeOn(MainActivity.this))
    {
        NativeLib.showSettingsAlert(MainActivity.this);
    }
    else
    {
      getLocationSettings();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多