How can I get the package name of the current launcher in android 2.3 and above?
Asked Answered
N

3

31

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

Nucleoplasm answered 27/4, 2012 at 4:42 Comment(1)
Doesn't that only give the package name of the current app ?Nucleoplasm
A
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;
Autogenesis answered 27/4, 2012 at 5:2 Comment(4)
If there is no selected "default" launcher it returns just "android". Not a common case, but it happens.Pita
Be aware that it will return NULL if there is no match and you will get a NPE at the last line!Lynnalynne
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 :)Autogenesis
For anyone wanting to run the block of code in this answer on an Android 11+ device, see my answer here about Android 11 package visibility changes which you should take into account.Malignant
M
4

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.

Malignant answered 1/11, 2021 at 18:57 Comment(0)
K
-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.

Kinsley answered 26/3, 2019 at 4:16 Comment(1)
That's the human readable name of the application, not the package name.Autogenesis

© 2022 - 2024 — McMap. All rights reserved.