当系统初始化完毕后会进入homeactivity:
具体代码流程:
ActivityManagerService.java -->
public void systemReady(final Runnable goingCallback) {
...
mMainStack.resumeTopActivityLocked(null);
...
}

ActivityStack.java
final boolean resumeTopActivityLocked(ActivityRecord prev) {
        return resumeTopActivityLocked(prev, null);
    }

final boolean resumeTopActivityLocked(ActivityRecord prev, Bundle options) {
...
return mService.startHomeActivityLocked(mCurrentUser); //开始启动homeactivity了,
...
}

然后我们可以在最开始加入一个自己写的接口,来设置默认launcher:
boolean startHomeActivityLocked(int userId) {
    ...
    setDefaultLauncher();
    ...

}

private void setDefaultLauncher() {          // get default component           String packageName = "com.android.launcher";//默认launcher包名        String className = "com.android.launcher2.Launcher";////默认launcher入口        Slog.i(TAG, "defautl packageName = " + packageName + ", default className = " + className);          if ((packageName != null && packageName.trim().length() > 1) && (className != null && className.trim().length() > 0)) {            boolean firstLaunch = SystemProperties.getBoolean("persist.sys.sw.firstLaunch", true);  //只做一次 可以注意这几个变量firstLaunch mFirstLaunch            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, "firstLaunch = " + firstLaunch);              if(mFirstLaunch){                  IPackageManager pm = ActivityThread.getPackageManager();                                    //清除当前默认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());                      }                }                                    //获取所有launcher activity                 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,UserHandle.getCallingUserId());                  }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();                  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] = new ComponentName(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;                  }                  //设置默认launcher                 ComponentName launcher = new ComponentName(packageName, className);                  try                   {                      pm.addPreferredActivity(filter, bestMatch, set, launcher,UserHandle.getCallingUserId());                  } catch (RemoteException e) {                      throw new RuntimeException("Package manager has died", e);                  }               }          }      } 


更多相关文章

  1. Android入门篇三:使用静态变量在Activity之间传递数据
  2. Android 初始化Setup Wizard——Provision
  3. android中实现全局变量
  4. adb环境变量配置
  5. Android全局变量
  6. Android 编译流程解析01-AppPlugin初始化
  7. Mac 下面,添加android adb命令(一般环境变量的添加方法)

随机推荐

  1. android文字滚动
  2. Linux/Android(安卓)Kernel Patch / Imag
  3. Android中自定义对话框小结
  4. Android: How to Capture Screen in Ging
  5. popwindow动画显示消失,activity切换动画
  6. Android(安卓)调用系统的分享[完美实现同
  7. Android基础知识总结(转载)
  8. 安卓4.X系统 增加蓝牙接收文件类型
  9. Android(安卓)代码设置Color的几种方式
  10. Android中自定义对话框小结