java 如何在android 2.3及以上版本中获取当前启动器的包名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10344824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:39:12  来源:igfitidea点击:

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

javaandroidlauncher

提问by HardCoder

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

如何在 Java 中以编程方式获取 android 2.3 及更高版本中当前启动器的包名称?

回答by JesusFreke

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

我认为您应该能够使用PackageManager.resolveActivity()和 home 意图。

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;

回答by childno?.de

in general, I agree to @JesusFrekeusing the PM resolveActivity

一般来说,我同意@JesusFreke使用 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

提示:如果没有默认设置,对于一般的系统意图接收器,这可能会变成“Android系统”或“打开”

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.

提示:如果您正在寻找网络浏览器,您可以使用net.openid.appauth.browser.BrowserSelector#select()( 0.7.1+) 来隐式获取默认浏览器,即使没有一个显式设置。