Android Intent Filter Package Visibility with <queries> Element
0 reputation · 18 May 2023, 18:41 UTC
0 reputation · 18 May 2023, 18:41 UTC
Android 12 (API 31) introduced stricter package visibility requirements to enhance user privacy. Applications targeting this API level or higher must declare the packages, intent filters, or providers they intend to interact with using the <queries> element in the AndroidManifest.xml.
When using implicit Intents to communicate between two separate applications, the sending app must have the appropriate visibility permissions to detect if a matching receiver exists. Without this declaration, methods like PackageManager.resolveActivity()` may return null even if a compatible application is installed on the device.
There is uncertainty regarding the most efficient way to declare visibility when the target application is not known by package name but is identified by a specific custom action string.
<intent-filter> within the <queries> block provide the same visibility as declaring a specific package name?Yes. Declaring an <intent-filter> inside the <queries> block gives the sending app the same visibility as an explicit <package> entry for the purpose of resolving implicit intents.
<queries> element is enforced; on lower API levels it is ignored.<intent-filter> entry makes the package manager treat any installed app whose manifest contains a matching <intent-filter> as visible to query APIs such as PackageManager.resolveActivity().<package> entry results in an O(1) lookup in the package manager’s package list.The visibility mechanism builds a temporary list of apps that satisfy the <queries> constraints. When the constraint is a package name, the list is assembled by a direct map lookup. When the constraint is an intent filter, the package manager must iterate over every installed package, parse its manifest intent‑filters, and compare them against the declared filter. This extra work explains the performance difference while preserving functional equivalence.
<queries> block that contains only an <intent-filter> for a custom action, e.g. <action android:name="com.example.CUSTOM_ACTION"/>.<intent-filter> matching that action.PackageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) where the intent specifies the custom action.ResolveInfo is returned when the receiver is present and null after the receiver is uninstalled.<package> entry in <queries> and verify the behavior is identical.Debug.startMethodTracing() or Systrace to compare explicit‑package vs intent‑filter queries on a device with a known number of installed apps.If you are targeting low‑end hardware or expect the device to have >500 installed apps, let me know the approximate app count so I can advise whether the scanning overhead of an intent‑filter query is acceptable for your use case.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.