在华为设备上设置默认 Android 启动器?

Set Default Android Launcher on Huawei devices?

我的目标是将我的应用设置为华为设备上的默认启动器。

1 - 解释:

1.1 - 当前情况:

我已经能够:

一切正常.. 华为设备除外!

从我的角度来看,华为的Android风格没有正确'honor' "ACTION_MANAGE_DEFAULT_APPS_SETTINGS" intent action contract。

// this displays the list of default apps on all tested devices, except on Huawei devices!
// instead, it does display apps permissions, app links and apps'advanced settings
intent.setAction(Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS);
activity.startActivity(intent);

作为 B 计划,我可以使用此显示 'Applications & Notifications' 设置 'page':

String packageName = "com.android.settings";
String className = "Settings$AppAndNotificationDashboardActivity";
intent.setClassName(packageName, packageName + "." + className);
activity.startActivity(intent);

因此用户可以从那里导航,按以下菜单项顺序:

我想避免这需要 2 或 3 个步骤。

1.2 - 这可以改进!

I found out that when the "-> Default Apps" menu item is selected, a (com.android.settings, .SubSettings) Intent (with extra) is launched但我没能做到这一点(权限被拒绝)。

但是我安装了Nova Launcher,结果在华为设备上可以显示“->默认应用”设置页面!
因此,用户登陆 she/he 只需点击“-> 默认启动器”然后选择默认启动器的页面:更容易。

2 - 问题:

因为我认为在华为设备上无法显示 'Lancher Picker',我的问题是:
如何在华为设备上显示“-> 默认应用程序”设置页面(图片在此处)(就像 Nova Launcher 一样)?
他们是否在华为设备上使用了其他意图操作?

预先感谢您的帮助。

是的,在华为设备上,Nova 使用不同的意图打开正确的屏幕。我可能是通过在从华为设备中提取的 Settings.apk 上使用 apktool 并查看 AndroidManifest 来发现这一点的。 请注意,"com.android" 始终是一种代码味道,因为它意味着它不是 public API 的一部分。此外,这甚至不是 "com.android",因为它在 AOSP 上不存在,com.android.settings.PREFERRED_SETTINGS 纯粹是华为的发明。很可能某些华为设备根本不会有这个。也有可能在未来这个意图可能会继续工作,但不会像现在那样做。所以慎重处理。

/* Some Huawei devices don't let us reset normally, handle it by opening preferred apps */
Intent preferredApps = new Intent("com.android.settings.PREFERRED_SETTINGS");
preferredApps.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
if (pm.resolveActivity(preferredApps, 0) != null) {
    context.startActivity(preferredApps);
} else {
    ...
}

事实上,接受的答案并非 100% 正确,因为它会打开一个通用的默认应用程序选择器 activity。

它有效,但最好让用户有权访问启动器选择器 activity — 它是 com.google.android.permissioncontroller/com.android.packageinstaller.role.ui.HomeSettingsActivity(至少对于 Android 10 Huawei Honors)。

所以,正确的代码片段是:

Intent()
            .apply {
                component = ComponentName("com.google.android.permissioncontroller", "com.android.packageinstaller.role.ui.HomeSettingsActivity")
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
            }
            .takeIf {
                packageManager.resolveActivity(it, 0) != null
            }
            ?.let(context::startActivity)