方法二:
在ActivityManagerService
boolean startHomeActivityLocked() {
/* launch the defualt launcher when the system boots for the first time */
setDefaultLauncher();
...
}private void setDefaultLauncher()
{
// get default component
String packageName = SystemProperties.get("ro.sw.defaultlauncherpackage", "no");
String className = SystemProperties.get("ro.sw.defaultlauncherclass", "no");
Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);
if(!packageName.equals("no") && !className.equals("no")){
boolean firstLaunch = SystemProperties.getBoolean("persist.sys.sw.firstLaunch", true);
Slog.d(TAG, "firstLaunch = " + firstLaunch);
if(firstLaunch){
mFirstLaunch = true;
// do this only for the first boot
SystemProperties.set("persist.sys.sw.firstLaunch", "false");
}
Slog.d(TAG, "mFirstLaunch = " + mFirstLaunch);
if(mFirstLaunch){
IPackageManager pm = ActivityThread.getPackageManager();
//clear the current preferred launcher
ArrayList intentList = new ArrayList();
ArrayList cnList = new ArrayList();
mContext.getPackageManager().getPreferredActivities(intentList, cnList, null);
IntentFilter dhIF;
for(int i = 0; i < cnList.size(); i++)
{
dhIF = intentList.get(i);
if(dhIF.hasAction(Intent.ACTION_MAIN) &&
dhIF.hasCategory(Intent.CATEGORY_HOME))
{
mContext.getPackageManager().clearPackagePreferredActivities(cnList.get(i).getPackageName());
}
}
// get all Launcher activities
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List list = new ArrayList();
try
{
list = pm.queryIntentActivities(intent,
intent.resolveTypeIfNeeded(mContext.getContentResolver()),
PackageManager.MATCH_DEFAULT_ONLY);
}catch (RemoteException e) {
throw new RuntimeException("Package manager has died", e);
}
// get all components and the best match
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
final int N = list.size();
ComponentName[] set = new ComponentName[N];
int bestMatch = 0;
for (int i = 0; i < N; i++)
{
ResolveInfo r = list.get(i);
set[i] = new ComponentName(r.activityInfo.packageName,
r.activityInfo.name);
if (r.match > bestMatch) bestMatch = r.match;
}
// add the default launcher as the preferred launcher
ComponentName launcher = new ComponentName(packageName, className);
try
{
pm.addPreferredActivity(filter, bestMatch, set, launcher);
} catch (RemoteException e) {
throw new RuntimeException("Package manager has died", e);
}
}
}
}