public static void setDefaultHome(Context context) {
PackageManager packageManager = context.getPackageManager();
final ComponentName component = Constants.HOME_COMPONENTNAME;
final List<ResolveInfo> homeActivities = new ArrayList<>();
packageManager.getHomeActivities(homeActivities);
final List<ComponentName> allComponents = new ArrayList<>();
for (ResolveInfo info : homeActivities) {
final ActivityInfo appInfo = info.activityInfo;
ComponentName activityName = new ComponentName(appInfo.packageName, appInfo.name);
allComponents.add(activityName);
}
// 利用 PackageManager 设置默认主页
packageManager.replacePreferredActivity(
HOME_FILTER,
IntentFilter.MATCH_CATEGORY_EMPTY,
allComponents.toArray(new ComponentName[0]),
component);
// Launch the new Home app so the change is immediately visible even if
// the Home button is not pressed.
// 跳转到主页
if (true) {
// 不能再次启动 Launcher,影响开机时间,再次启动会多耗时 4s+
return;
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivityAsUser(intent, UserHandle.CURRENT);
}