In Android 11, we can see a lot of updates that improves privacy. If your app uses the PackageManager
methods to get the list of installed apps in the user’s device, you will have to make some changes in your code for devices using Android 11. In this blog post we will be discussing all our options.
If you are already querying user’s installed apps, the following code snippet will look familiar. This is how you can get a list of installed apps of the user.
Now for your users using Android 11, the code remains the same but it won’t work unless you add some additional elements in the AndroidManifest
.
There are 3 different ways of querying installed apps of the user in Android 11. Let’s have a look at them:
Query specific packages
If you already know which apps you want to query just mention the package names inside the <queries>
element in the AndroidManifest
.
Query using intent filter
In case you don’t know all the package names of the apps that you want to query but there is a set of apps with a similar functionality that you want to query then you can use an intent filter inside the <queries>
element according to your requirements like it has been done in the code snippet below.
The <intent>
element looks like <intent-filter>
but there are few differences. <intent>
element has the following restrictions:
- The
<intent>
element can have only one<action>
element. - The
<data>
element can only have the following attributes :mimeType
,scheme
andhost
.
Query all the apps
If you want to query all the apps of the user like you were doing earlier, you need to include QUERY_ALL_PACKAGES
permission in the AndroidManifest
. It is a normal
permission and it is granted as soon as the app is installed.
Ideally one should request the least amount of packages and respect user’s privacy. In most cases this permission won’t be required, only for apps like launchers it makes sense to ask the user for permission to query all the installed apps in their phone.
There is one loop hole that I noticed while exploring the <queries>
element if you add android.intent.action.MAIN
as the action
element in the intent filter, you can see almost all the apps of the user without adding the permission since almost all apps would have this element in the AndroidManifest
.
Thanks for reading! If you enjoyed this story, please click the 👏 button and share it to help others!
If you have any kind of feedback, feel free to connect with me on Twitter.