Android开发Launcher进程启动流程

 更新时间:2023年06月20日 10:31:41   作者:安安_660c  
这篇文章主要为大家介绍了Android开发Launcher进程启动流程示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

1、Launcher

Launcher作为Android系统的桌面,它的作用有两点:
作为Android系统的启动器,用于启动应用程序;
作为Android系统的桌面,用于显示和管理应用程序的快捷图标或者其它桌面组件;

2、Launcher进程启动流程

2.1、SystemServer调用

在SystemServer进程启动之后,执行其run()函数,在里面执行了大量的配置设置操作,并且启动了各种引导服务、核心服务以及其他服务等,包括AMS、PMS、WMS、电量管理服务等一系列服务,以及创建主线程Looper,并循环等待消息;

其中在启动引导服务方法中,启动了ActivityManagerService,并且在启动其他服务的方法中,调用AMS的systemReady()方法,Launcher进程就是从这儿开始启动的;

public final class SystemServer {
    private void run() {
    ...
    startBootstrapServices();
    startOtherServices();
    ...
  }
  private void startBootstrapServices() {
    ...
    mActivityManagerService = mSystemServiceManager.startService(ActivityManagerService.Lifecycle.class).getService();
    mActivityManagerService.setSystemServiceManager(mSystemServiceManager);
    mActivityManagerService.setInstaller(installer);
    ...
  }
  private void startOtherServices() {
    ...
    mActivityManagerService.systemReady(() -> { 
    }, BOOT_TIMINGS_TRACE_LOG);
  }
}

在SystemServer启动的时候,执行startOtherServices()方法中,里面调用了AMS的systemReady()方法,通过该方法来启动Launcher;

// Tag for timing measurement of main thread.
private static final String SYSTEM_SERVER_TIMING_TAG = "SystemServerTiming";
private static final TimingsTraceLog BOOT_TIMINGS_TRACE_LOG
            = new TimingsTraceLog(SYSTEM_SERVER_TIMING_TAG, Trace.TRACE_TAG_SYSTEM_SERVER);
private void startOtherServices() {
  ...
  mActivityManagerService.systemReady(() -> {
    Slog.i(TAG, "Making services ready");
    traceBeginAndSlog("StartActivityManagerReadyPhase");
    mSystemServiceManager.startBootPhase(SystemService.PHASE_ACTIVITY_MANAGER_READY);
    ...
  }, BOOT_TIMINGS_TRACE_LOG);
}

2.2、AMS执行

在AMS中执行systemReady()方法,在其中执行startHomeActivityLocked()方法,传入当前用户ID;

public void systemReady(final Runnable goingCallback, TimingsTraceLog traceLog) {
  ...
  synchronized (this) {
    ...
    startHomeActivityLocked(currentUserId, "systemReady");
    ...
  }
  ...
}

2.2.1、获取Launcher的Intent

在startHomeActivityLocked()方法中,首先通过getHomeIntent()方法,获取到要启动的HomeActivity的intent对象,其中mTopAction默认为INTENT.ACTION_MAIN,并添加CATEGORY_HOME的category标志;

得到Intent对象,通过PackageManager去获取对应符合的Activity,获取对应的ActivityInfo,并获取对应的进程记录,此时对应的进程还没启动,后面继续执行,为intent添加FLAG_ACTIVITY_NEW_TASK启动参数,开启新栈,随后调用ActivityStartController类的startHomeActivity()方法去执行启动;

boolean startHomeActivityLocked(int userId, String reason) {
  ...
  Intent intent = getHomeIntent(); 
  ActivityInfo aInfo = resolveActivityInfo(intent, STOCK_PM_FLAGS, userId);
  if (aInfo != null) {
    intent.setComponent(new ComponentName(aInfo.applicationInfo.packageName, aInfo.name));
    // Don't do this if the home app is currently being instrumented.
    aInfo = new ActivityInfo(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);
    }
  }
  ...
  return true;
}
Intent getHomeIntent() {
  Intent intent = new Intent(mTopAction, mTopData != null ? Uri.parse(mTopData) : null);
  intent.setComponent(mTopComponent);
  intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING);
  if (mFactoryTest != FactoryTest.FACTORY_TEST_LOW_LEVEL) {
    intent.addCategory(Intent.CATEGORY_HOME);
  }
  return intent;
}

2.2.2、启动Launcher

在startHomeActivity()方法中,调用obtainStarter()方法获取到一个ActivityStarter对象,setCallingUid()方法设置当前调用的Uid=0,然后执行其execute()方法;

void startHomeActivity(Intent intent, ActivityInfo aInfo, String reason) {
  mSupervisor.moveHomeStackTaskToTop(reason);
  mLastHomeActivityStartResult = obtainStarter(intent, "startHomeActivity: " + reason)
    .setOutActivity(tmpOutRecord)
    .setCallingUid(0)
    .setActivityInfo(aInfo)
    .execute();
  mLastHomeActivityStartRecord = tmpOutRecord[0];
  if (mSupervisor.inResumeTopActivity) {
    // If we are in resume section already, home activity will be initialized, but not
    // resumed (to avoid recursive resume) and will stay that way until something pokes it
    // again. We need to schedule another resume.
    mSupervisor.scheduleResumeTopActivities();
  }
}

在ActivityStarter的execute()方法中,mayWait默认为false,执行startActivity()方法;

int execute() {
  try {
    // TODO(b/64750076): Look into passing request directly to these methods to allow
    // for transactional diffs and preprocessing.
    if (mRequest.mayWait) {
      return startActivityMayWait(mRequest.caller, mRequest.callingUid,  ...);
    } else {
      return startActivity(mRequest.caller, mRequest.intent, mRequest.ephemeralIntent, ...);
    }
  } finally {
    onExecutionComplete();
  }
}

这里进入了Activity的启动流程,Launcher本身就是一个系统APP,用于显示桌面等,LauncherApp启动之后会执行其生命周期方法初始化桌面布局;

2.3、初始化桌面图标

2.3.1、执行onCreate()方法

@Override
protected void onCreate(Bundle savedInstanceState) {
  ...
  LauncherAppState app = LauncherAppState.getInstance(this);
  ...
}

获取LauncherAppState,通过LauncherAppState的getInstance()方法获取,该方法里面会判断当前线程是否为主线程,在主线程时还会直接new出对象,不在主线程时,通过MainThreadExecutor的submit()方法向主线程提交一个任务去获取该对象;

// We do not need any synchronization for this variable as its only written on UI thread.
private static LauncherAppState INSTANCE;
public static LauncherAppState getInstance(final Context context) {
  if (INSTANCE == null) {
    if (Looper.myLooper() == Looper.getMainLooper()) {
      INSTANCE = new LauncherAppState(context.getApplicationContext());
    } else {
      try {
        return new MainThreadExecutor().submit(new Callable<LauncherAppState>() {
          @Override
          public LauncherAppState call() throws Exception {
            return LauncherAppState.getInstance(context);
          }
        }).get();
      } catch (InterruptedException|ExecutionException e) {
        throw new RuntimeException(e);
      }
    }
  }
  return INSTANCE;
}

2.3.2、读取安装APP信息

在LauncherAppState的构造方法中,会新建InvariantDeviceProfile对象,这个类主要是存储App的基本配置信息,例如App图标的尺寸大小,文字大小,每个工作空间或文件夹能显示多少App等;
在LauncherAppState的构造方法中,会获取WindowManager,并获取屏幕的尺寸,解析桌面布局文件,获取默认尺寸信息等;

@TargetApi(23)
public InvariantDeviceProfile(Context context) {
  WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  Display display = wm.getDefaultDisplay();
  DisplayMetrics dm = new DisplayMetrics();
  display.getMetrics(dm);
  ...
  ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
  ...
}
ArrayList<InvariantDeviceProfile> getPredefinedDeviceProfiles(Context context) {
  ArrayList<InvariantDeviceProfile> profiles = new ArrayList<>();
  try (XmlResourceParser parser = context.getResources().getXml(R.xml.device_profiles)) {
    final int depth = parser.getDepth();
    int type;
    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
      if ((type == XmlPullParser.START_TAG) && "profile".equals(parser.getName())) {
        TypedArray a = context.obtainStyledAttributes(Xml.asAttributeSet(parser), R.styleable.InvariantDeviceProfile);
        int numRows = a.getInt(R.styleable.InvariantDeviceProfile_numRows, 0);
        int numColumns = a.getInt(R.styleable.InvariantDeviceProfile_numColumns, 0);
        float iconSize = a.getFloat(R.styleable.InvariantDeviceProfile_iconSize, 0);
        profiles.add(new InvariantDeviceProfile(
          a.getString(R.styleable.InvariantDeviceProfile_name),
          a.getFloat(R.styleable.InvariantDeviceProfile_minWidthDps, 0),
          a.getFloat(R.styleable.InvariantDeviceProfile_minHeightDps, 0),
          numRows,
          numColumns,
          a.getInt(R.styleable.InvariantDeviceProfile_numFolderRows, numRows),
          a.getInt(R.styleable.InvariantDeviceProfile_numFolderColumns, numColumns),
          iconSize,
          a.getFloat(R.styleable.InvariantDeviceProfile_landscapeIconSize, iconSize),
          a.getFloat(R.styleable.InvariantDeviceProfile_iconTextSize, 0),
          a.getInt(R.styleable.InvariantDeviceProfile_numHotseatIcons, numColumns),
          a.getResourceId(R.styleable.InvariantDeviceProfile_defaultLayoutId, 0),
          a.getResourceId(R.styleable.InvariantDeviceProfile_demoModeLayoutId, 0)));
        a.recycle();
      }
    }
  } catch (IOException|XmlPullParserException e) {
    throw new RuntimeException(e);
  }
  return profiles;
}

2.3.3、注册Intent广播

新建LauncherModel对象,该对象是一个BroadcastReceiver,并添加App变化的回调,以及设置Filter并注册广播,用于监听桌面App的变化;

private LauncherAppState(Context context) {
  ...
  mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
  LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
  // Register intent receivers
  IntentFilter filter = new IntentFilter();
  filter.addAction(Intent.ACTION_LOCALE_CHANGED);
  // For handling managed profiles
  filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
  filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
  ...
  mContext.registerReceiver(mModel, filter);
  ...
}
public class LauncherModel extends BroadcastReceiver ... {}

2.3.4、解析Launcher布局

继续回到Launcher的onCreate()方法,将Launcher添加到LauncherModel中,是以弱引用的方式添加,初始化一些其工作,解析Launcher的布局,

2.3.5、加载桌面

onCreate()方法中,通过LauncherModel的startLoader()来加载桌面App;

@Override
protected void onCreate(Bundle savedInstanceState) {
  ...
  if (!mModel.startLoader(currentScreen)) {
    if (!internalStateHandled) {
      // If we are not binding synchronously, show a fade in animation when
      // the first page bind completes.
      mDragLayer.getAlphaProperty(ALPHA_INDEX_LAUNCHER_LOAD).setValue(0);
    }
  } else {
    // Pages bound synchronously.
    mWorkspace.setCurrentPage(currentScreen);
    setWorkspaceLoading(true);
  }
  ...
}

在LauncherModel的startLoader()方法中,新建了一个LoaderResults对象,并通过startLoaderForResults()方法创建出一个LoaderTask的Runnable任务,将其在工作线程中执行起来;

public boolean startLoader(int synchronousBindPage) {
  ...
  synchronized (mLock) {
    // Don't bother to start the thread if we know it's not going to do anything
    if (mCallbacks != null &amp;&amp; mCallbacks.get() != null) {
      ...
      LoaderResults loaderResults = new LoaderResults(mApp, sBgDataModel, mBgAllAppsList, synchronousBindPage, mCallbacks);
      if (mModelLoaded &amp;&amp; !mIsLoaderTaskRunning) {
        ...
        return true;
      } else {
        startLoaderForResults(loaderResults);
      }
    }
  }
  return false;
}
public void startLoaderForResults(LoaderResults results) {
  synchronized (mLock) {
    stopLoader();
    mLoaderTask = new LoaderTask(mApp, mBgAllAppsList, sBgDataModel, results);
    runOnWorkerThread(mLoaderTask);
  }
}
private static void runOnWorkerThread(Runnable r) {
  if (sWorkerThread.getThreadId() == Process.myTid()) {
    r.run();
  } else {
    // If we are not on the worker thread, then post to the worker handler
    sWorker.post(r);
  }
}

在LoaderTask的run()方法中,去加载手机已安装的App的信息,查询数据库获取已安装的App的相关信息,加载Launcher布局,并将数据转化为View,绑定到界面上,由此我们就可以看到桌面显示的宫格列表的桌面图标了;

public void run() {
  ...
  try (LauncherModel.LoaderTransaction transaction = mApp.getModel().beginLoader(this)) {
    // 查询数据库整理App信息,转化为View绑定到界面
    loadWorkspace();
    mResults.bindWorkspace();
    loadAllApps();
    mResults.bindAllApps();
    loadDeepShortcuts();
    mResults.bindDeepShortcuts();
    mBgDataModel.widgetsModel.update(mApp, null);
    mResults.bindWidgets();
    transaction.commit();
  } catch (CancellationException e) {
    // Loader stopped, ignore
    TraceHelper.partitionSection(TAG, "Cancelled");
  }
  TraceHelper.endSection(TAG);
}

以上就是Android开发Launcher进程启动流程的详细内容,更多关于Android Launcher启动的资料请关注脚本之家其它相关文章!

相关文章

  • Flutter配置代理抓包实现过程详解

    Flutter配置代理抓包实现过程详解

    这篇文章主要为大家介绍了Flutter配置代理抓包实现过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • 详解Flutter如何获取Text截断后的字符串

    详解Flutter如何获取Text截断后的字符串

    当Text文本设置maxLins属性将文本强制截断之后,Text的承载字符串是截断前,还是截断后的呢,我们又该如何获取截断后的字符串呢,下面就来和大家详细讲讲
    2023-06-06
  • Android View的事件分发机制深入分析讲解

    Android View的事件分发机制深入分析讲解

    事件分发从手指触摸屏幕开始,即产生了触摸信息,被底层系统捕获后会传递给Android的输入系统服务IMS,通过Binder把消息发送到activity,activity会通过phoneWindow、DecorView最终发送给ViewGroup。这里就直接分析ViewGroup的事件分发
    2023-01-01
  • Android开发中ViewPager实现多页面切换效果

    Android开发中ViewPager实现多页面切换效果

    ViewPager用于实现多页面的切换效果,该类存在于Google的兼容包里面,所以在引用时记得在BuilldPath中加入“Android-support-v4.jar”。具体详情大家可以参考下本文
    2016-11-11
  • android listview的多列模版实例代码

    android listview的多列模版实例代码

    这篇文章主要介绍了android listview的多列模版实例代码的相关资料,这里附有实例代码,具有参考价值,需要的朋友可以参考下
    2017-01-01
  • Android实现简单的下拉刷新pulltorefresh

    Android实现简单的下拉刷新pulltorefresh

    这篇文章主要为大家详细介绍了Android实现简单的下拉刷新pulltorefresh的相关代码,具有一定的实用性和操作价值,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android如何在root设备上开启ViewServer详解

    Android如何在root设备上开启ViewServer详解

    这篇文章主要给大家介绍了关于Android中如何在root设备上开启ViewServer的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2017-12-12
  • Input系统之InputReader处理按键事件详解

    Input系统之InputReader处理按键事件详解

    这篇文章主要为大家介绍了Input系统之InputReader处理按键事件详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android WebView实现全屏播放视频

    Android WebView实现全屏播放视频

    WebView是Android系统中的原生控件,其主要功能与前端页面进行响应交互,快捷省时地实现如期的功能,相当于增强版的内置浏览器。这篇文章主要介绍的是利用WebView实现全屏播放视频的功能,感兴趣的小伙伴可以了解一下
    2021-12-12
  • Android自定义ViewGroup之CustomGridLayout(一)

    Android自定义ViewGroup之CustomGridLayout(一)

    这篇文章主要为大家详细介绍了Android自定义ViewGroup之CustomGridLayout的相关资料,感兴趣的小伙伴们可以参考一下
    2016-09-09

最新评论