Android组件Activity的启动流程源码解析

在Android开发中,Activity是最常用的组件之一。Activity的启动流程比较复杂,涉及到多个系统组件和类的协同工作。本文将对Activity的启动流程进行源码解析。

  1. startActivity()

Activity的启动是通过调用startActivity()方法实现的。这个方法主要做了以下几件事情:

  1. 构造一个Intent对象,包含要启动的Activity的信息。
  2. 调用ActivityManagerService的startActivity()方法,将Intent对象传递给AMS。
  3. AMS根据Intent对象中的信息,找到要启动的Activity的信息。
  4. AMS启动ActivityThread线程,并将Activity的信息传递给它。
  5. ActivityThread线程根据Activity的信息,创建Activity对象,并调用它的onCreate()方法,最终将Activity显示在屏幕上。

下面我们逐一解析这个过程。

  1. Intent

Intent是一个Android中非常重要的组件。它可以用来启动Activity、Service、BroadcastReceiver等组件。在Activity的启动过程中,Intent主要用来传递Activity的信息。Intent包含以下信息:

  1. ComponentName:要启动的Activity的类名。
  2. Action:要执行的操作。
  3. Data:要操作的数据。
  4. Category:要启动的Activity所属的类别。

Intent的构造方法如下:

public Intent(Context packageContext, Class<?> cls) {
this(packageContext, cls, null);
}

public Intent(Context packageContext, Class<?> cls, String action) {
mComponent = new ComponentName(packageContext, cls);
setAction(action);
}

在这个构造方法中,我们传入了要启动的Activity的类名和所属的包名。然后通过ComponentName类将它们组合成一个组件。最后通过setAction()方法设置要执行的操作。

  1. AMS

ActivityManagerService(AMS)是Android系统中一个非常重要的服务。它负责管理应用程序的进程、Activity、Service等组件。在Activity的启动过程中,AMS主要负责以下几个工作:

  1. 根据Intent对象中的信息,找到要启动的Activity的信息。
  2. 根据Activity的信息,创建一个新的进程或者将Activity所在的进程调到前台。
  3. 启动ActivityThread线程,并将Activity的信息传递给它。

AMS的startActivity()方法的实现如下:

public int startActivity(IApplicationThread caller, Intent intent,
String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int flags, ProfilerInfo profilerInfo, Bundle options) {

boolean componentSpecified = intent.getComponent() != null;
if (!componentSpecified && intent.resolveActivity(mService.getPackageManager()) == null) {
if (showActivityError(caller, token, "No Activity found to handle "
+ intent.toString())) {
res = ActivityManager.START_RETURN_INTENT_TO_CALLER;
} else {
// If we are not allowing this for the caller then we
// just immediately finish, letting the caller know
// they can’t do this.
return ActivityManager.START_CANCELED;
}
} else {
try {
/*
* The activity is not found, so we need to get the
* activity to run from somewhere. In this case, we
* will use the application package’s default activity
* if there is no activity specified in the Intent.
*/
ActivityInfo aInfo = mSupervisor.resolveActivity(intent, resolvedType, 0, null, userId);

// Check whether the activity should be launched in a new task.
final int res = startActivityLocked(caller, intent, resolvedType, aInfo,
resultTo, resultWho, requestCode, callingPid, callingUid, callingPackage, realCallingPid,
realCallingUid, startFlags, options, componentSpecified, null, null);

} catch (RuntimeException e) {

}
}

}

在AMS的startActivity()方法中,首先通过resolveActivity()方法找到要启动的Activity的信息。如果找不到Activity的信息,就会抛出异常。如果找到Activity的信息,就会调用startActivityLocked()方法启动Activity。

  1. ActivityThread

ActivityThread是Android系统中一个非常重要的线程。它负责管理Activity的生命周期,以及处理与Activity相关的消息。在Activity的启动过程中,ActivityThread主要负责以下几个工作:

  1. 根据Activity的信息,创建Activity对象。
  2. 调用Activity对象的onCreate()方法,初始化Activity。
  3. 将Activity添加到WindowManager中,显示在屏幕上。

ActivityThread的main()方法是整个应用程序的入口。它会创建一个Looper对象,并通过它来接收消息。在Activity的启动过程中,ActivityThread会处理以下消息:

  1. LAUNCH_ACTIVITY:启动Activity。
  2. CREATE_SERVICE:创建Service。
  3. BIND_SERVICE:绑定Service。
  4. RECEIVER:处理BroadcastReceiver。

ActivityThread的main()方法的实现如下:

public static void main(String[] args) {

Looper.prepareMainLooper();

ActivityThread thread = new ActivityThread();
thread.attach(false);

Looper.loop();

}

在ActivityThread的attach()方法中,会创建一个Application对象和一个Instrumentation对象。然后根据Activity的信息,创建一个Activity对象,并调用它的onCreate()方法。最后将Activity添加到WindowManager中,显示在屏幕上。

  1. 总结

通过以上分析,我们可以看出,Activity的启动流程非常复杂,涉及到多个系统组件和类的协同工作。在实际开发中,我们只需要关注startActivity()方法即可,剩下的工作都是由Android系统自动完成的。但是,了解Activity的启动流程对于我们理解Android系统的运作原理是非常有帮助的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值