【问题标题】:flutter location , Unhandled Exception: PlatformException(PERMISSION_DENIED_NEVER_ASK, Background location permission denied forever颤振位置,未处理的异常:PlatformException(PERMISSION_DENIED_NEVER_ASK,后台位置权限永远被拒绝
【发布时间】:2021-12-31 21:22:26
【问题描述】:

我正在使用Locationflutter 包来访问用户的后台位置,任何用户第一次打开应用程序时都会请求权限,当用户接受时,它会在控制台中返回此错误

Unhandled Exception: PlatformException(PERMISSION_DENIED_NEVER_ASK, Background location permission denied forever - please open app settings, null, null)

如果用户关闭应用程序并重新打开它......它工作得非常好(在前台和后台获取位置),甚至无需再次请求位置许可。

按照包本身中的入门指南,这是我向我的AndroidManifest.xml 文件添加权限的方法:

  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>

【问题讨论】:

  • 您找到解决此问题的方法了吗?
  • @AhmedElsayed 不,太糟糕了,我不得不强制重启应用程序,所以在第二次运行时权限流程正常
  • 谢谢!你试过地理定位器吗?有同样的问题吗?
  • 好的,Geolocator 也有同样的问题,但我找到了解决方案并在下面发布。

标签: android flutter location


【解决方案1】:

我一直在研究这个问题,我发现问题是当你使用 location.enableBackgroundMode() 然后选择 always 当你第一次回击时它会抛出异常,isBackgroundModeEnabled 也会是错误的“尽管后台模式已在系统中启用”,您必须重新启动应用程序以检查后台模式是否正确启用。

但是,我发现再次调用location.enableBackgroundMode() 也可以解决问题,它实际上并没有要求再次启用背景,但它以某种方式使isBackgroundModeEnabled 返回真值。这是我的修复代码:

Future<bool> enableBackgroundMode() async {
bool _bgModeEnabled = await location.isBackgroundModeEnabled();
if (_bgModeEnabled) {
  return true;
} else {
  try {
    await location.enableBackgroundMode();
  } catch (e) {
    debugPrint(e.toString());
  }
  try {
    _bgModeEnabled = await location.enableBackgroundMode();
  } catch (e) {
    debugPrint(e.toString());
  }
  print(_bgModeEnabled); //True!
  return _bgModeEnabled;
 }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-26
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2021-09-15
    • 2017-05-23
    • 2021-08-01
    相关资源
    最近更新 更多