写点什么

Android 设置默认 Launcher

用户头像
Android架构
关注
发布于: 1 小时前

当系统中有多个 Launcher 桌面时,可通过以下方式设定默认的 Launcher:


方法一:




偷懒的话,将不想要的 Launcher,如 Launcher3 的 AndroidManifest.xml 文件中的 HOME 属性去掉,只保留自己想要的 Launcher 即可,前提是我们有其他 Launcher 的源码。弊端是去掉 Home 属性后再也无法进入。


方法二:




1.编辑:


frameworks/base/services/java/com/android/server/pm/PackageManagerService.java


文件中的


public void systemReady() {}函数,在最后添加以下代码段:


// TChip ZJ Add Below for Default Launcher


if (isFirstBoot()) {


// 修改为需要设置的 package name


String examplePackageName = "com.tchip.carlauncher";


// 修改为需要设置的 launcher activity name


String exampleActivityName = "com.tchip.carlauncher.ui.activity.MainActivity";


Intent intent = new Intent(Intent.ACTION_MAIN);


intent.addCategory(Intent.CATEGORY_HOME);


fin


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


al int callingUserId = UserHandle.getCallingUserId();


List<ResolveInfo> resolveInfoList = queryIntentActivities(intent,


null, PackageManager.GET_META_DATA, callingUserId);


if (resolveInfoList != null) {


int size = resolveInfoList.size();


for (int j = 0; j < size;) {


final ResolveInfo r = resolveInfoList.get(j);


if (!r.activityInfo.packageName.equals(examplePackageName)) {


resolveInfoList.remove(j);


size -= 1;


} else {


j++;


}


}


ComponentName[] set = new ComponentName[size];


ComponentName defaultLauncher = new ComponentName(


examplePackageName, exampleActivityName);


int defaultMatch = 0;


for (int i = 0; i < size; i++) {


final ResolveInfo resolveInfo = resolveInfoList.get(i);


set[i] = new ComponentName(


resolveInfo.activityInfo.packageName,


resolveInfo.activityInfo.name);


if (defaultLauncher.getClassName().equals(


resolveInfo.activityInfo.name)) {


defaultMatch = resolveInfo.match;


}


}


IntentFilter filter = new IntentFilter();


filter.addAction(Intent.ACTION_MAIN);


filter.addCategory(Intent.CATEGORY_HOME);


filter.addCategory(Intent.CATEGORY_DEFAULT);


addPreferredActivity2(filter, defaultMatch, set,


defaultLauncher);


}


}


// TChip ZJ Add Above for Default Launcher


2.添加以下函数:


/**


  • TChip ZJ Add for Default Launcher


*/


public void addPreferredActivity2(IntentFilter filter, int match,


ComponentName[] set, ComponentName activity) {


Log.d("debug_default", "addPreferredActivity2 is called.");


// writer


synchronized (mPackages) {


Slog.i(TAG, "Adding preferred activity " + activity + ":");


filter.dump(new LogPrinter(Log.INFO, TAG), " ");


mSettings.editPreferredActivitiesLPw(0).addFilter(


new PreferredActivity(filter, match, set, activity, true));


scheduleWriteSettingsLocked();


}


}


3.修改 PackageManagerService.java 的函数 findPreferredActivity:


(1)对 removeMatches 进行条件判断,在 launcher 时不 remove:


if (!(intent.getAction() != null


&& intent.getAction().equals(intent.ACTION_MAIN)


&& intent.getCategories() != null && intent.getCategories()


.contains(intent.CATEGORY_HOME))) {


Slog.d(TAG, "launcher");


} else {


if (removeMatches) {


pir.removeFilter(pa);


if (DEBUG_PREFERRED) {


Slog.v(TAG, "Removing match " + pa.mPref.mComponent);


}


break;


}


}


(2)

用户头像

Android架构

关注

还未添加个人签名 2021.10.31 加入

还未添加个人简介

评论

发布
暂无评论
Android 设置默认Launcher