android 清单文件中的 Flutter 位置权限问题

Flutter location permission issues in android manifest file

我的 Flutter 代码:

 onPressed: () async {
                  Geolocator
                      .getCurrentPosition(desiredAccuracy: LocationAccuracy.best, forceAndroidLocationManager: true)
                      .then((Position position) {
                    setState(() {
                      _currentPosition = position;
                      print ( "LAT: ${_currentPosition.latitude}, LNG: ${_currentPosition.longitude}");
                    });
                  }).catchError((e) {
                    print(e);
                  });
                },

它显示以下内容:

`No location permissions are defined in the manifest. Make sure at least CCESS_FINE_LOCATION` or ACCESS_COARSE_LOCATION are defined in the manifest.

甚至以为我已经在清单中添加了权限

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.agent">
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
   <application
        android:label="agent"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

我有以下无法修复的错误:

此处不允许属性 android:icon 未解决 class 'MainActivity'

首先,尝试gradle sync。按照建议 :

  1. Sync gradle from android studio (must required step)
  2. flutter clean
  3. flutter pub get
  4. flutter run

其次,如果还是不行,可能是. To solve this, please use permission_handler package to request the Permission.location或其他类似的权限导致的,在你真正使用geo插件之前。

所需的准确性:LocationAccuracy.best 两者都需要

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我在下面添加一些信息到

您可以查看geolocator package docs。主要讲2点

  • 对于 AndroidX 版本的 Android 支持库,添加 2 个属性并检查 SDK 版本。
  • 添加位置权限.

如果您处于 Android +10 并且“如果您想要继续接收更新,即使您的应用 运行 在后台运行 ”到文档,你必须添加这个权限:

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

仔细检查文档。

在里面添加这行

android->app->src->main->AndroidManifest.xml

android->app->src->profile->AndroidManifest.xml

android->app->src->debug->AndroidManifest.xml

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

<!-- Recommended for Android 9 (API level 28) and lower. -->
<!-- Required for Android 10 (API level 29) and higher. -->
<service
   android:name="MyNavigationService"
   android:foregroundServiceType="location" ... >
<!-- Any inner elements would go here. -->
</service>

Read this article