请尊重原创,转载请注明出处 tianyl_Melodie 的博客,http://blog.csdn.net/tianyl_melodie/article/details/53424116
1、Context和它的子类
在Android中,也被称为上下文,运行环境,通过context可以拿到Android运行时的一些信息,所以我们今天先从Context开始看。通过看Context,我们可以发现它其实是一个抽象类,里面定义了一下我们经常使用的方法:
代码片段
public abstract AssetManager getAssets();
public abstract Resources getResources();
public abstract PackageManager getPackageManager();
public abstract ContentResolver getContentResolver();
既然context只是一个抽象类,定义了一些方法,并没有对应的实现,那么就需要通过它的子类才能知道对应的实现。在Android中,context的子类有非常多,如图:
看到这么多类可能一时半会有点晕,改从哪个类开始看起呢。既然这样,那就从我们用得最多的一个类,Activity开始看起吧。通过观察,我们可以看到,Activity是继承的ContextThemeWrapper,而ContextThemeWrapper又是继承ContextWrapper,然后ContextWrapper才是继承自Context的。其实上图已经清晰的展现出了Activity的父类结构。
那么是不是Context的实现方法都写在ContextWrapper这个类里面呢,我们打开ContextWrapper这个类看一下,可以发现,他所有的方法,都是通过调用一个对象来完成的,代码片段如下:
public AssetManager getAssets() {
return mBase.getAssets();
}
@Override
public Resources getResources(){
return mBase.getResources();
}
@Override
public PackageManager getPackageManager() {
return mBase.getPackageManager();
}
@Override
public ContentResolver getContentResolver() {
return mBase.getContentResolver();
}
而这个mBase对象,则是最开始通过
ContextWrapper的构造方法传进来的,也就是说
Context的实现类并不是
ContextWrapper,通过这个类的名字,我们也可以理解,它只是一个
Context的包装类。那么
Context的实现到底在哪呢?
通过名字,我们也可以看出来,ContextImpl就是
Context的实现类。
其实,在创建Activity之前,系统会通过ActivityManagerService这个类来初始化一些信息,这其中就包括对ActivityThread中Context的初始化,在这个时候,如果ContextImpl的对象还没有创建,那么就会创建出对应的ContextImpl对象,并作为成员变量进行保存起来。这样,以后在用到Context进行创建对象的时候,就会将这个对象传入。
就是通过这样,所以就变成了现在这样,ContextImpl实现Context的抽象方法,ContextWrapper和它的子类,通过持有ContextImpl的对象,调用对应的方法。对应的UML图如下:
通过查看ContextImpl,我们可以发现,虽说它是Context的实现类,但并非所有的实现方法都写在这个类中。比如有些就是通过PackageInfo这个对象获取的返回值,比如我们经常用到的一个方法:getApplicationContext()。
@Override
public Context getApplicationContext() {
return (mPackageInfo != null) ?
PackageInfo.getApplication() : mMainThread.getApplication();
}
通过查看PackageInfo,我们可以发现它其实就是一个LoadedApk类。关于LoadedApk这个类以后学习的时候再细说,我们可以先认为Context的方法都是在ContextImpl这个类中进行实现的即可,还是回归正题。
2、ActivityThread
首先,在启动一个应用程序的时候,首先是会在Android的ActivityManagerService中,调用ActivityThread的main方法。之前我们学习了ActivityThread的main方法中,创建Looper的过程,现在来看ActivityThread的main方法中的另外一行代码。
public static void main(String[] args) {
SamplingProfilerIntegration.start();
// CloseGuard defaults to true and can be quite spammy. We
// disable it here, but selectively enable it later (via
// StrictMode) on debug builds, but using DropBox, not logs.
CloseGuard.setEnabled(false);
Environment.initForCurrentUser();
// Set the reporter for event logging in libcore
EventLogger.setReporter(new EventLoggingReporter());
Security.addProvider(new AndroidKeyStoreProvider());
Process.setArgV0("<pre-initialized>");
//构建主线程中的MessageQueue和Looper对象
Looper.prepareMainLooper();
//构建ActivityThread对象
ActivityThread thread = new ActivityThread();
thread.attach(false);
if (sMainThreadHandler == null) {
sMainThreadHandler = thread.getHandler();
}
AsyncTask.init();
if (false) {
Looper.myLooper().setMessageLogging(new
LogPrinter(Log.DEBUG, "ActivityThread"));
}
Looper.loop();
throw new RuntimeException("Main thread loop unexpectedly exited");
}
之前我们看的是main中对Looper对象的构建,在构建完Looper对象之后,会执行这两行代码:
ActivityThread thread = new ActivityThread();
thread.attach(false);
首先是创建一个ActivityThread对象,这时就会初始化
ActivityThread类中的一些成员。然后调用
thread.attach(false)方法。至于这个方法做了哪些事情,我们先点进去看。
private void attach(boolean system) {
sCurrentActivityThread = this;
mSystemThread = system;
if (!system) {
ViewRootImpl.addFirstDrawHandler(new Runnable() {
@Override
public void run() {
ensureJitEnabled();
}
});
android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
UserHandle.myUserId());
RuntimeInit.setApplicationObject(mAppThread.asBinder());
IActivityManager mgr = ActivityManagerNative.getDefault();
try {
mgr.attachApplication(mAppThread);
} catch (RemoteException ex) {
// Ignore
}
} else {
// Don't set application object here -- if the system crashes,
// we can't display an alert, we just want to die die die.
android.ddm.DdmHandleAppName.setAppName("system_process",
UserHandle.myUserId());
try {
mInstrumentation = new Instrumentation();
ContextImpl context = new ContextImpl();
context.init(getSystemContext().mPackageInfo, null, this);
Application app = Instrumentation.newApplication(Application.class, context);
mAllApplications.add(app);
mInitialApplication = app;
app.onCreate();
} catch (Exception e) {
throw new RuntimeException(
"Unable to instantiate Application():" + e.toString(), e);
}
}
// add dropbox logging to libcore
DropBox.setReporter(new DropBoxReporter());
ViewRootImpl.addConfigCallback(new ComponentCallbacks2() {
@Override
public void onConfigurationChanged(Configuration newConfig) {
synchronized (mResourcesManager) {
// We need to apply this change to the resources
// immediately, because upon returning the view
// hierarchy will be informed about it.
if (mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null)) {
// This actually changed the resources! Tell
// everyone about it.
if (mPendingConfiguration == null ||
mPendingConfiguration.isOtherSeqNewer(newConfig)) {
mPendingConfiguration = newConfig;
queueOrSendMessage(H.CONFIGURATION_CHANGED, newConfig);
}
}
}
}
@Override
public void onLowMemory() {
}
@Override
public void onTrimMemory(int level) {
}
});
}
这里的代码有点多,我们可以简单的看一下,在main方法,中,我们通过thread.attach(false)调用到了这里,此时的判断,"!system"为true,执行了这样一行代码:
IActivityManager mgr = ActivityManagerNative.getDefault();
这里,通过这个方法,拿到的其实是一个ActivityManagerService远程代理对象,我们将通过它和
ActivityManagerService进行通信,这里就不深入研究了,待到以后学习
ActivityManagerService再看。
mgr.attachApplication(mAppThread);
在拿到这个代理对象之后,将mAppThread传了进去。我们可以简单理解,在应用程序启动的时候,首先会通过
ActivityManagerService调用到
ActivityThread的main(),然后在main()中,创建了一个
ActivityThread对象,并初始化了它的成员变量,然后拿到了
ActivityManagerService的远程代理对象,将成员变量
mAppThread传了进去。看到这里,
ActivityManagerService和
ActivityThread通信的关联,已经基本完成了。
3、ApplicationThread
mAppThread是ApplicationThread这个类的实例,在创建ActivityThread时,就创建了这个对象,之后又将它传入了ActivityManagerService的远程代理对象,可以理解ActivityManagerService和ActivityThread是通过ApplicationThread进行通信的。我们仔细查看这个类,发现它是继承自ApplicationThreadNative。而ApplicationThreadNative类实现了IApplicationThread这个接口,我们查看这个接口对象:
代码片段:
void schedulePauseActivity(IBinder token, boolean finished, boolean userLeaving,
int configChanges) throws RemoteException;
void scheduleStopActivity(IBinder token, boolean showWindow,
int configChanges) throws RemoteException;
void scheduleWindowVisibility(IBinder token, boolean showWindow) throws RemoteException;
void scheduleSleeping(IBinder token, boolean sleeping) throws RemoteException;
void scheduleResumeActivity(IBinder token, int procState, boolean isForward)
throws RemoteException;
void scheduleCreateService(IBinder token, ServiceInfo info,
CompatibilityInfo compatInfo, int processState) throws RemoteException;
void scheduleBindService(IBinder token,
Intent intent, boolean rebind, int processState) throws RemoteException;
void scheduleUnbindService(IBinder token,
Intent intent) throws RemoteException;
上面代码是
IApplicationThread接口中定义的一些,方法,我们可以发现它其实都是一下对activity生命周期的管理。而
ApplicationThreadNative这个类则实现了这些管理生命周期的方法,通过在
onTransact方法中,调用对应的方法,代码如下:
@Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
throws RemoteException {
switch (code) {
case SCHEDULE_PAUSE_ACTIVITY_TRANSACTION:
{
data.enforceInterface(IApplicationThread.descriptor);
IBinder b = data.readStrongBinder();
boolean finished = data.readInt() != 0;
boolean userLeaving = data.readInt() != 0;
int configChanges = data.readInt();
schedulePauseActivity(b, finished, userLeaving, configChanges);
return true;
}
case SCHEDULE_STOP_ACTIVITY_TRANSACTION:
{
data.enforceInterface(IApplicationThread.descriptor);
IBinder b = data.readStrongBinder();
boolean show = data.readInt() != 0;
int configChanges = data.readInt();
scheduleStopActivity(b, show, configChanges);
return true;
}
......
当然,它也只是一个抽象类,具体的实现方法是实现是它的子类中,比如:
ApplicationThreadProxy和ApplicationThread
。
ApplicationThreadProxy看名字可以猜测它是一个代理类,其实他
的实现方式是通过
IBinder进行的,这块以后在学习IBinder的时候再仔细学习
,当然它并不是我们今天的重点,我们回到今天的重点
ApplicationThread。
刚才在 ApplicationThreadNative这个类中,我们已经看到了,它是通过一个 onTransact方法调用不同的方法进行不同的逻辑处理的,比如说我们想启动一个Activity,那么它会执行里面的这样的逻辑:
case SCHEDULE_LAUNCH_ACTIVITY_TRANSACTION:
{
data.enforceInterface(IApplicationThread.descriptor);
Intent intent = Intent.CREATOR.createFromParcel(data);
IBinder b = data.readStrongBinder();
int ident = data.readInt();
ActivityInfo info = ActivityInfo.CREATOR.createFromParcel(data);
Configuration curConfig = Configuration.CREATOR.createFromParcel(data);
CompatibilityInfo compatInfo = CompatibilityInfo.CREATOR.createFromParcel(data);
int procState = data.readInt();
Bundle state = data.readBundle();
List<ResultInfo> ri = data.createTypedArrayList(ResultInfo.CREATOR);
List<Intent> pi = data.createTypedArrayList(Intent.CREATOR);
boolean notResumed = data.readInt() != 0;
boolean isForward = data.readInt() != 0;
String profileName = data.readString();
ParcelFileDescriptor profileFd = data.readInt() != 0
? ParcelFileDescriptor.CREATOR.createFromParcel(data) : null;
boolean autoStopProfiler = data.readInt() != 0;
scheduleLaunchActivity(intent, b, ident, info, curConfig, compatInfo, procState, state,
ri, pi, notResumed, isForward, profileName, profileFd, autoStopProfiler);
return true;
}
在上面代码中,我们可以看到这样一行:
scheduleLaunchActivity(intent, b, ident, info, curConfig, compatInfo, procState, state,
ri, pi, notResumed, isForward, profileName, profileFd, autoStopProfiler);
刚才我们已经说了,
ApplicationThreadNative只是一个抽象类,
ApplicationThread才是实现类,那么现在,通过这行代码,就会回调到
ApplicationThread中,我们看看在
ApplicationThread中是如何实现启动Activity的流程的。
下面就是启动Activity的逻辑:
public final void scheduleLaunchActivity(Intent intent, IBinder token, int ident,
ActivityInfo info, Configuration curConfig, CompatibilityInfo compatInfo,
int procState, Bundle state, List<ResultInfo> pendingResults,
List<Intent> pendingNewIntents, boolean notResumed, boolean isForward,
String profileName, ParcelFileDescriptor profileFd, boolean autoStopProfiler) {
updateProcessState(procState, false);
ActivityClientRecord r = new ActivityClientRecord();
r.token = token;
r.ident = ident;
r.intent = intent;
r.activityInfo = info;
r.compatInfo = compatInfo;
r.state = state;
r.pendingResults = pendingResults;
r.pendingIntents = pendingNewIntents;
r.startsNotResumed = notResumed;
r.isForward = isForward;
r.profileFile = profileName;
r.profileFd = profileFd;
r.autoStopProfiler = autoStopProfiler;
updatePendingConfiguration(curConfig);
queueOrSendMessage(H.LAUNCH_ACTIVITY, r);
}
private void queueOrSendMessage(int what, Object obj, int arg1, int arg2) {
synchronized (this) {
if (DEBUG_MESSAGES) Slog.v(
TAG, "SCHEDULE " + what + " " + mH.codeToString(what)
+ ": " + arg1 + " / " + obj);
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
msg.arg1 = arg1;
msg.arg2 = arg2;
mH.sendMessage(msg);
}
}
final H mH = new H();
再看一下H类的定义,我们可以发现,它其实就是一个Handler对象:
private class H extends Handler
4、Activity的启动过程
public void handleMessage(Message msg) {
if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
switch (msg.what) {
case LAUNCH_ACTIVITY: {
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
ActivityClientRecord r = (ActivityClientRecord)msg.obj;
r.packageInfo = getPackageInfoNoCheck(
r.activityInfo.applicationInfo, r.compatInfo);
handleLaunchActivity(r, null);
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
} break;
......
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
unscheduleGcIdler();
if (r.profileFd != null) {
mProfiler.setProfiler(r.profileFile, r.profileFd);
mProfiler.startProfiling();
mProfiler.autoStopProfiler = r.autoStopProfiler;
}
handleConfigurationChanged(null, null);
if (localLOGV) Slog.v(
TAG, "Handling launch of " + r);
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
r.createdConfig = new Configuration(mConfiguration);
Bundle oldState = r.state;
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed);
if (!r.activity.mFinished && r.startsNotResumed) {
try {
r.activity.mCalled = false;
mInstrumentation.callActivityOnPause(r.activity);
if (r.isPreHoneycomb()) {
r.state = oldState;
}
if (!r.activity.mCalled) {
throw new SuperNotCalledException(
"Activity " + r.intent.getComponent().toShortString() +
" did not call through to super.onPause()");
}
} catch (SuperNotCalledException e) {
throw e;
} catch (Exception e) {
if (!mInstrumentation.onException(r.activity, e)) {
throw new RuntimeException(
"Unable to pause activity "
+ r.intent.getComponent().toShortString()
+ ": " + e.toString(), e);
}
}
r.paused = true;
}
} else {
try {
ActivityManagerNative.getDefault()
.finishActivity(r.token, Activity.RESULT_CANCELED, null);
} catch (RemoteException ex) {
// Ignore
}
}
}
Activity a = performLaunchActivity(r, customIntent);
然后,再是调用Activity的Resume方法:
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed);
最后则是调用
Activity的
OnPause
方法:
mInstrumentation.callActivityOnPause(r.activity);
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
......
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
if (r.state != null) {
r.state.setClassLoader(cl);
}
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component
+ ": " + e.toString(), e);
}
}
try {
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
if (activity != null) {
Context appContext = createBaseContextForActivity(r, activity);
CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Configuration config = new Configuration(mCompatConfiguration);
if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
+ r.activityInfo.name + " with config " + config);
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config);
if (customIntent != null) {
activity.mIntent = customIntent;
}
r.lastNonConfigurationInstances = null;
activity.mStartedActivity = false;
int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
mInstrumentation.callActivityOnCreate(activity, r.state);
......
}
r.paused = true;
mActivities.put(r.token, r);
} catch
......
return activity;
}
首先,系统会拿到Activity的类加载器,然后通过newInstance方法创建出Activity对象:
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
然后,系统会拿到对应的Application对象:
Application app = r.packageInfo.makeApplication(false, mInstrumentation);
最后,系统才会调用
Activity的OnCreate方法:
mInstrumentation.callActivityOnCreate(activity, r.state);
看到这里,我们也基本清楚了一个应用,从进程启动,到Activity创建的过程。