boolean startHomeActivityLocked(int userId, String reason){setDefaultLauncher();//调用设置默认launcher方法if(mFactoryTest == FactoryTest.FACTORY_TEST_LOW_LEVEL
&& mTopAction == null){// We are running in factory test mode, but unable to find// the factory test app, so just sit around displaying the// error message and don't try to start anything.returnfalse;}
Intent intent =getHomeIntent();
ActivityInfo aInfo =resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);if(aInfo != null){
intent.setComponent(newComponentName(aInfo.applicationInfo.packageName, aInfo.name));// Don't do this if the home app is currently being// instrumented.
aInfo =newActivityInfo(aInfo);
aInfo.applicationInfo =getAppInfoForUser(aInfo.applicationInfo, userId);
ProcessRecord app =getProcessRecordLocked(aInfo.processName,
aInfo.applicationInfo.uid,true);if(app == null || app.instr == null){
intent.setFlags(intent.getFlags()| FLAG_ACTIVITY_NEW_TASK);
final int resolvedUserId = UserHandle.getUserId(aInfo.applicationInfo.uid);// For ANR debugging to verify if the user activity is the one that actually// launched.
final String myReason = reason +":"+ userId +":"+ resolvedUserId;
mActivityStartController.startHomeActivity(intent, aInfo, myReason);}}else{
Slog.wtf(TAG,"No home screen found for "+ intent,newThrowable());}returntrue;}//设置默认launcher的方法privatevoidsetDefaultLauncher(){
boolean mFirstLaunch =true;
String packageName ="com.xxxxx.smartlauncher";//launcher包名
String className ="Launcher";if((packageName != null && packageName.trim().length()>1)&&(className != null && className.trim().length()>0)){if(mFirstLaunch){
IPackageManager pm = ActivityThread.getPackageManager();
ArrayList<IntentFilter> intentList =new ArrayList<IntentFilter>();
ArrayList<ComponentName> cnList =new ArrayList<ComponentName>();
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());}}
Intent intent =newIntent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> list =new ArrayList<ResolveInfo>();try{
list = pm.queryIntentActivities(intent,
intent.resolveTypeIfNeeded(mContext.getContentResolver()),
PackageManager.MATCH_DEFAULT_ONLY,UserHandle.getCallingUserId()).getList();//add .getList()}catch(RemoteException e){thrownewRuntimeException("Package manager has died", e);}// get all components and the best match
IntentFilter filter =newIntentFilter();
filter.addAction(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
final int N = list.size();
Slog.d(TAG,"N:::::hyhyhyhy:::: = "+ N);
ComponentName[] set =new ComponentName[N];int bestMatch =0;for(int i =0; i < N; i++){
ResolveInfo r = list.get(i);
set[i]=newComponentName(r.activityInfo.packageName,
r.activityInfo.name);
Slog.d(TAG,"r.activityInfo.packageName:::::hyhyhyhy:::: = "+ r.activityInfo.packageName);
Slog.d(TAG,"r.activityInfo.name:::::hyhyhyhy:::: = "+ r.activityInfo.name);if(r.match > bestMatch) bestMatch = r.match;}//<C9><E8><D6><C3>?<C8><CF>launcher
ComponentName launcher =newComponentName(packageName, className);try{
Log.d("zy","pm.addPreferredActivity");
pm.addPreferredActivity(filter, bestMatch, set, launcher,UserHandle.getCallingUserId());}catch(RemoteException e){thrownewRuntimeException("Package manager has died", e);}}}}//end