Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PackageManager not returning correct packages with targetSdkVersion 30

I am using the PackageManager to get a list of all packages installed on the user's device. This is working perfectly fine, until I switch from targetSdkVersion 29 to 30.

When I increase the targetSdkVersion from 29 to 30, the PackageManager is not returning the correct list of packages anymore (I'm making a launcher and in fact, it is barely returning any packages that can be launched).

I tried pm.getInstalledPackages(0), pm.getInstalledApplications(0) and the method for retrieving apps as indicated here. None of them worked, and all of them were working previously.

The build.gradle version settings:

compileSdkVersion 30
defaultConfig {
    minSdkVersion 23
    targetSdkVersion 29
}

Does anyone have an idea of what is happening here?

like image 865
Jorn Rigter Avatar asked Jan 24 '26 17:01

Jorn Rigter


2 Answers

You need to add a declaration to your manifest in order to see other packages when targeting Android 11 (API 30): https://developer.android.com/about/versions/11/privacy/package-visibility

In particular, if you're building a launcher,

<manifest>
  <queries>
    <intent>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent>
  </queries>
  ...
</manifest>

would allow all results that

Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.getPackageManager().queryIntentActivities(intent, 0);

could match to be returned.

like image 183
ephemient Avatar answered Jan 27 '26 05:01

ephemient


As @ephemient pointed out in his comment of his answer, using <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/> solved it.

This is done because of the restricted access apps have from Android 11 onwards, as this article explains well.

like image 32
Jorn Rigter Avatar answered Jan 27 '26 06:01

Jorn Rigter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!