链接.canOpenURL在React Native上定位Android 30 SDK时返回假

11dmarpk  于 2022-12-09  发布在  Android
关注(0)|答案(5)|浏览(142)

最近将我们的构建目标切换到android 30以添加对Android 11的支持,现在方向和电话呼叫不起作用。
文档中提到了本地深度链接和/或npm包的使用。这是本地完成的吗?还是包是一个选项?
https://reactnative.dev/docs/linking
在运行android 30时,检查是否支持Linking.canOpenURL时,它现在返回false。

Linking.canOpenURL(url)
.then((supported: boolean) => (supported ? Linking.openURL(url) : Alert.alert("Error", "There was an error attempting to opening the location.")))
.catch(() => Alert.alert("Error", "There was an error attempting to opening the location."));
lnvxswe2

lnvxswe21#

答案就在于一项全新的Android安全功能:Package Visibility。这与iOS的LSApplicationQueriesSchemes类似。
定位Android 11(SDK 30)需要你更新你的AndroidManifest.xml,并包含你要查询的应用列表。例如,下面是我在自己的应用中用于检查谷歌Map导航的代码。它还包括HTTP和HTTPS的权限:

<manifest package="com.example.game">
  <queries>
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="http"/>
    </intent>
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="https"/>
    </intent>
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="geo" />
    </intent>
    <intent>
      <action android:name="android.intent.action.VIEW" />
      <data android:scheme="google.navigation" />
    </intent>
  </queries>
  ...
</manifest>

对于applications matching certain criterias(例如防病毒应用、文件管理器或浏览器),您可以使用QUERY_ALL_PACKAGES权限来允许查询任何随机包,类似于早期Android版本:

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

但是,请注意,如果您不符合下列任何应用类型的资格,或者以未经授权的方式使用QUERY_ALL_PACKAGES,您的应用将被Google Play拒绝
许可使用涉及必须发现设备上任何和所有已安装应用程序的应用程序,出于认知或互操作性目的,这些应用程序可能有资格获得许可。许可使用包括设备搜索、防病毒应用程序、文件管理器和浏览器。被授予此权限的应用程序必须遵守用户数据政策,包括显著披露和同意要求。不得将其用于未公开或无效的目的。
请参阅使用广泛包(应用程序)可见性(QUERY_ALL_PACKAGES)权限

aurhwmvo

aurhwmvo2#

在我的例子中,我需要将android:host="*"添加到data标记中,如下所示:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="com.acme">
...
    <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" android:host="*" />
        </intent>
    </queries>
</manifest>

对于Linking.canOpenURL(url),返回truehttps://... URL。

u59ebvdq

u59ebvdq3#

如果您决定使用

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

谷歌说:
许可使用涉及必须发现设备上任何和所有已安装应用程序的应用程序,出于认知或互操作性目的,这些应用程序可能有资格获得许可。许可使用包括设备搜索、防病毒应用程序、文件管理器和浏览器。被授予此权限的应用程序必须遵守用户数据政策,包括显著披露和同意要求。不得将其用于未公开或无效的目的。
从Google Play更新策略

v1l68za4

v1l68za44#

如果您使用expo,则可以通过执行以下操作将此(https://github.com/chirag04/react-native-mail/pull/175/files)添加到manifest文件:
1.在根文件夹中创建一个名为“android-manifest.plugin.js”的新文件,其中包含下一个代码。

const { withAndroidManifest } = require("@expo/config-plugins")

module.exports = function androiManifestPlugin(config) {
  return withAndroidManifest(config, async config => {
    let androidManifest = config.modResults.manifest

    androidManifest["queries"].push({
        "intent": [{
            "action": [{
                "$": {
                    "android:name": "android.intent.action.SEND_MULTIPLE"
                }
            }],
            "data": [{
                "$": {
                    "android:mimeType": "*/*"
                }
            }]
        }]
    });

    return config;
  })
}

1.将上述插件添加到您的expo配置文件app.json

{
  "expo": {
    ...,
    "plugins": [
      "./android-manifest.plugin.js"
    ]
  }
}

1.使用eas build重建,也可以使用classic(expo build),但请记住,它很快就会被弃用
就是这样。你的APK应该有正常的清单加上上面链接中提到的部分。你也可以使用这种方法来设置你的清单。你也可以使用这个工具来验证你的清单是否被成功修改(如何从APK文件中查看AndroidManifest.xml?)

3qpi33ja

3qpi33ja5#

通过在清单中添加此内容解决了问题

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

谢谢大家的支持。

相关问题