Android API 30包可见性<query>标签所需Intent类型及配置疑问
Android API 30 包可见性配置问题解答
疑问解答
- 问题1:结论正确。
是AndroidManifest.xml中 标签的直接子元素,必须和 标签平级,不能放在 标签内部。 - 问题2:你混淆了
和组件 的作用。 的作用是声明你的应用需要查询系统中哪些其他应用的组件信息,和你自身应用的Activity配置完全无关,你自己Activity的 不需要做任何移动,继续保留在Activity标签下即可,系统不会将 中的规则和你自身的组件做绑定。 - 问题3:不需要多次声明
标签。全局只需要一个 根标签,所有需要查询的Intent规则都作为 子元素放在同一个 标签内即可,即使有多个包含BROWSABLE的匹配规则,也只要按需添加多个 子元素就行。 - 问题4:该写法完全合规。
标签内的标准子元素就是 ,用来声明你要查询的Intent匹配规则;而 是用来声明自身组件可以响应的Intent规则,二者作用场景完全不同,开发者的写法是正确的。
修正后可运行完整代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.test"> <!-- 包可见性声明,和application平级 --> <queries> <!-- 适配Usage1:查询ACTION_VIEW类型的Intent对应组件 --> <intent> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.BROWSABLE" /> <!-- 若只需查询特定scheme的结果,可补充对应data声明 --> <data android:scheme="xxx" /> <data android:scheme="https" /> </intent> <!-- 适配Usage2:查询ACTION_SEND类型text/plain格式的Intent对应组件 --> <intent> <action android:name="android.intent.action.SEND" /> <data android:mimeType="text/plain" /> </intent> </queries> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Test"> <activity android:name=".TestActivity" android:label="@string/app_name" android:launchMode="singleTask" android:screenOrientation="portrait" android:windowSoftInputMode="adjustPan" android:exported="true"> <!-- 修复原代码中文引号问题 --> <intent-filter> <data android:host="open" android:scheme="xxx" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> </intent-filter> <intent-filter android:autoVerify="true" tools:targetApi="m"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:host="www.xxx.com" android:pathPrefix="/" android:scheme="https" /> </intent-filter> </activity> <activity android:name=".ABCActivity" android:screenOrientation="portrait" android:windowSoftInputMode="adjustResize" android:exported="true"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> </intent-filter> </activity> </application> </manifest>
额外说明
- Usage3获取自身应用版本名的逻辑不需要额外声明
规则,应用自身的包信息默认可见。 - 补充了Activity的
android:exported属性,API 31及以上要求所有包含的组件必须显式声明exported属性,避免编译报错。
内容的提问来源于stack exchange,提问作者user14016240
相关产品推荐
相关产品推荐

