android - 从我的应用程序打开 android 自己的启动器

标签 android launcher homescreen package-managers

您好,这个简单的问题已经找了 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/

相关文章:

android - 如何重新排序 android 中的 Activity ?

java - 将Android应用程序的快捷方式添加到主屏幕点击按钮

java - 如何修复 androidx.appcompat.widget.AppCompatButton 无法转换为 com.rey.material.widget.Button

android - Intent 在默认启动器中打开默认桌面屏幕

java - 将 Android 启动器快捷方式添加到主屏幕

重启期间主屏幕启动器应用程序不会调用 Android 应用程序 onCreate

ios - iOS 8 iPhone 主屏幕上应用程序图标下方应用程序名称旁边的红点(圆圈)

android - 在 KitKat 4.4.2 中获取 SDard(s) 路径和大小

java - 无法在 <28 Api 上运行为 Api 28(使用 minSdk 21)制作的应用程序

android - android list 文件中main,default和launcher的用途是什么