项目背景需求:
1、我们的系统要固定一个HOME应用,并且用户无法通过安装其他Launcher3应用来更改;
2、在生产过程中,通过不同的初始化,进入不同的Launcher应用;
根据以上的需求,我们先来理一理Launcher应用的流程:
1、Android在进行各种服务启动之后运行
((ActivityManagerService)ActivityManagerNative.getDefault()).systemReady()
在systemReady后开始开始启动Launcher。
frameworks\base\services\Java\com\android\server\am\ActivityManagerService.java
frameworks\base\services\java\com\android\server\am\ActivityManagerService.java
frameworks/base/core/java/android/content/Intent.java
根据上面代码可知,在寻找Launcher的时候是根据HOME的filter(在Manifest中定义的<category android:name=”android.intent.category.HOME” />)来过滤。 然后根据filter出来的HOME来启动,如果只有一个HOME,则启动这个HOME,如果用户自己装了HOME,那就会弹出来一个列表供用户选择。
了解了以上流程后,现在我的思路就比较简单:
1、在Intent.java中定义一个获取我们自己的CATEGORY_HOME的接口:
public static final String CATEGORY_MYHOME = "android.intent.category.MYHOME";
private static String mMyCategoryHome=CATEGORY_MYHOME;
void setMyCategoryHome(String str){
mMyCategoryHome = str;
}
String getMyCategoryHome(){
return CATEGORY_MYHOME;
}
2、接下来,搜索系统中所有用到Intent.CATEGORY_HOME的位置,改为使用Intent.getMyCategoryHome()来获取即可;
3、对应将我们自己的Launcher中的
- <category android:name="android.intent.category.HOME" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.MYHOME" />
- <category android:name="android.intent.category.DEFAULT" />
即可;
4、对于需要多种状态下对应不同的Launcher的情况,可以通过调用setMyCategoryHome()接口灵活的进行切换,匹配不同的定制Launcher应用