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

后端 未结 2 1932
-上瘾入骨i
-上瘾入骨i 2020-12-08 07:02

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

相关标签:
2条回答
  • 2020-12-08 07:50

    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;
    
    0 讨论(0)
  • 2020-12-08 08:02

    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.

    0 讨论(0)
提交回复
热议问题