访问隐藏方法的问题。 Android、颤振、地理定位器

Problem with Accessing hidden method. Android, Flutter, Geolocator

我正在 Udemy 上这个 Flutter/Dart 课程,在开始一个新项目时,我一直在尝试使用 Geolocater Package. The initial github project for that is located here

课程的第一步是导入 Geolocator 包,将其添加到 pubspec.yml,然后在 screens/loading_screen.dart 文件中的 _LoadingScreenState 中添加一个函数:

void getLocation() async {
  Position position = await Geolocator()
      .getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
  print(position);
}

最后您只需添加 getLocation();到同一页上的 onPressed。在本教程中,这很好用,因为我相信他们使用的是 API 26 和 Geolocator 3.0.1 版。但是,我使用的是 API 29 或 30(均无效),但出现以下错误:

W/ppbrewery.clim(25370): Accessing hidden method Landroid/content/Context;->getFeatureId()Ljava/lang/String; (greylist, reflection, allowed)

我对这个问题做了一些研究,它似乎是这样做的,因为我在 API 30。但是,我不知道该怎么做或如何让它打印我的位置。

查看一些文档:

https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

底部写着:如何启用对非 SDK 接口的访问?

但是 adb shell settings put global hidden_api_policy 1 命令对我没有任何作用。那可能是因为我在 API 30,但我根本不知道。它似乎也没有在 API 29.

上做任何事情

此外,上面的解决方案似乎只是为了修复它在 dev device/emulator,除非我误解了它。我该如何修复模拟器和未来的实际部署?

试试这个。

Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true;
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.low);

您可以通过点击模拟器末尾的三个点来打开此设置

并且不要忘记像这样在模拟器上设置位置并设置位置。

我在 android 模拟器中遇到了同样的问题。在模拟器位置设置中,您必须设置位置,然后在模拟器中打开 google 地图并定位自己。之后重新启动应用程序,应该没问题。

获取 Geolocator 实例强制 Android 位置管理器并在此调用 getCurrentPosition 之后。通过这种方式,您可以使用 LocationManager 插件而不是默认的 FusedLocationProviderClient 插件来检索位置。

Geolocator geolocator = Geolocator()..forceAndroidLocationManager = true;
Position position = await geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.low);