您好,这个简单的问题已经找了 2 天了。我想从我的应用程序启动 Android 自己的启动器,即使它没有设置为默认启动器。
final PackageManager packageManager=getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage("com.android.launcher");
这对于 Android 自己的启动器返回 null 但如果我尝试自定义启动器是成功的给我
最佳答案
在调查了getLaunchIntentForPackage的源代码后找到了解决方案.根据文档,
The current implementation will look first for a main activity in the category CATEGORY_INFO, next for a main activity in the category CATEGORY_LAUNCHER, or return null if neither are found.
因此,该函数不查找 CATEGORY_HOME,我按以下方式重写了它,它运行良好。
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
intentToResolve.addCategory(Intent.CATEGORY_HOME);
intentToResolve.setPackage("com.android.launcher");
ResolveInfo ri = getPackageManager().resolveActivity(intentToResolve, 0);
if (ri != null)
{
Intent intent = new Intent(intentToResolve);
intent.setClassName(ri.activityInfo.applicationInfo.packageName, ri.activityInfo.name);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
关于android - 从我的应用程序打开 android 自己的启动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518768/