繁体   English   中英

当Android设备处于飞行模式时,打开位置提示失败

[英]Turn on location prompt fails when android device is in flight mode

上下文我有这个应用程序,它要求用户在进行任何活动之前先打开位置,因为我们在移动时记录位置,但是,我的一些用户选择在移动时保持飞行模式。我所有的用户都使用Android 4.4 Samsung 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.
}

上面的代码提示用户单击“确定”以打开位置信息,因此用户不必离开应用程序并转到设置即可打开位置信息。 在检查需要位置的其他应用程序时,我还注意到,由于与Google地图使用的位置检查相同,因此在飞行模式和位置​​关闭时。Google地图也不会提醒用户打开位置并仅告诉他们检查他们的互联网连接。关于最好的方法来始终检查位置设置的任何想法将受到高度赞赏,谢谢

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

        //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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM