31

How can I get the package name of the current launcher in android 2.3 and above programmatically in Java ?

HardCoder
  • 3,026
  • 6
  • 32
  • 52

3 Answers3

68

I think you should be able to use PackageManager.resolveActivity(), with the home intent.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String currentHomePackage = resolveInfo.activityInfo.packageName;
JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • 4
    If there is no selected "default" launcher it returns just "android". Not a common case, but it happens. – Amir Uval Apr 20 '15 at 12:08
  • 1
    Be aware that it will return NULL if there is no match and you will get a NPE at the last line! – JacksOnF1re Jun 18 '15 at 09:15
  • 2
    Yes, this is illustratory code to illustrate the solution, and should not be used as-is for "production" use. Besides, if your phone doesn't have a home application... you probably have other problems :) – JesusFreke Jun 18 '15 at 21:24
  • 1
    For anyone wanting to run the block of code in this answer on an Android 11+ device, see my answer [here](https://stackoverflow.com/a/69801531/1071320) about Android 11 package visibility changes which you should take into account. – Adil Hussain Nov 01 '21 at 18:59
3

With the package visibility changes introduced in Android 11, it is now necessary to add a queries element in your application's manifest file as below before you can query the PackageManager.resolveActivity(intent:flags:) method for the default home (a.k.a. launcher) activity that is installed on the device:

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

If this queries element is omitted from your application's manifest, then the device will report the com.android.settings.FallbackHome activity as its default home activity and that is most likely not what you want.

For guidance on how to query the PackageManager.resolveActivity(intent:flags:) method, see the accepted answer in this thread.

Adil Hussain
  • 30,049
  • 21
  • 112
  • 147
-2

in general, I agree to @JesusFreke using the PM resolveActivity

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

but to get the right package name, you should use

resolveInfo.loadLabel(packageManager).toString()

or

resolveInfo.activityInfo.applicationInfo.loadLabel(packageManager).toString()

Hint: if there is no default set, this might become "Android System" or "open" as for the general System Intent Receiver

Hint: if you're looking for web browsers, you might use net.openid.appauth.browser.BrowserSelector#select() (0.7.1+) to implicit get the default browser even there is no one explicit set.

childno͡.de
  • 4,679
  • 4
  • 31
  • 57