【问题标题】:Remove background location access on Android删除 Android 上的后台位置访问权限
【发布时间】:2021-02-08 12:30:25
【问题描述】:

我正在使用react-native-geolocation-service 来获取用户的位置。在 iOS 上,当应用程序在前台时很容易请求位置访问,您只需跳过添加后台位置访问作为一项功能。但是,我不明白您如何在 Android 上删除此权限。我没有ACCESS_BACKGROUND_LOCATION 权限集,我仍然可以在例如“始终允许”选项。我的像素 2。

这是我用来获取用户位置的代码:

const getCurrentPosition = async (onSuccess, onError) => {
  if (Platform.OS === 'android') {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        title: 'My title',
        message: 'My message',
        buttonPositive: 'Continue',
      }
    );
    if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
      onError();
      return;
    }
  }
  Geolocation.getCurrentPosition(
    (result) => {
      const position = {
        longitude: result.coords.longitude,
        latitude: result.coords.latitude,
      };
      onSuccess(position);
    },
    (error) => {
      onError();
    },
    { enableHighAccuracy: true, maximumAge: 10000, timeout: 15000 }
  );
};

【问题讨论】:

  • 丹尼尔,你解决了这个问题吗?我自己也有同样的问题。
  • 很遗憾没有:/

标签: android react-native geolocation


【解决方案1】:

查看您的主要AndroidManifest.xml 文件(我指的是android/app/src/main(COULD_BE_SOMETHING_ELSE_USUALLY_MAIN_BUT_YMMV)/AndroidManifest.xml 下的主要文件)

你需要找到一堆<uses-permission />。检查其中一个坏男孩是否获得了 ACCESS_BACKGROUND_LOCATION:

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

运气不好?不要害怕,我亲爱的朋友。这里有一个补救办法,可以解决你因与反应原生的交易而疲惫不堪的头脑。在最后一个&lt;uses-permission&gt; 标签下添加这一行。

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

愿和平盛行。

技术细节:

实际上,像react-native-geolocation-servicereact-native-background-geolocation 这样的库可以更改最终的AndroidManifest.xml,这将转到经过大量编辑的捆绑包。

仅仅删除一行不会删减它,因为您无权访问该最终草稿,因为它是自动生成的。您需要领先一步并使用tools:node="remove" 确保它将从清单文件的最终版本中删除。

来源:

【讨论】:

  • 您可能需要在清单标头中添加 xmlns:tools="schemas.android.com/tools"
  • 谢谢。您能否详细说明或提供一个链接说明为什么要这样做?
  • 添加“删除”指令的原因是从您的应用程序中删除此权限,即使您的某些依赖项/库有时会使用它。例如,当依赖项中的某个功能需要此权限时,但您不使用该功能。删除它意味着您的应用不需要此权限,因此无需确认 Google 对使用后台位置权限的应用的要求。
  • @PerChristianHenden 感谢您的回复!我在@AlxVallejo 评论中特别询问了xmlns:tools="schemas.android.com/tools" 位。也许,你可以回答这个问题?
  • 是的,xmlns:tools语句是导入tools命名空间,以便后面的语句可以使用tools:node。
猜你喜欢
  • 1970-01-01
  • 2020-08-21
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 2019-05-01
  • 2021-01-29
  • 1970-01-01
相关资源
最近更新 更多