ActivityThread并不是一个Thread,而是一个单纯的Java类,查看一下 ActivityThread 的源码final class ActivityThread ,并没有继承Thread或者实现Runnable接口,ActivityThread 其中包含 main 方法,程序的入口地方,怎么看出来的呢? 我们开发过程中总会出现程序异常信息,细心看一下log,查看最下面的几行,最终问题出在 android.app.ActivityThread.main.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5133) AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:141) AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) AndroidRuntime: at android.os.Looper.loop(Looper.java:137) AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103) AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525) AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
介绍相关几个类的结构: class ApplicationThread extends ApplicationThreadNative abstract class ApplicationThreadNative extends Binder implements IApplicationThread ApplicationThread (Binder)对象。其中 Binder负责接收远程AMS的 IPC调用,接收到调用 后,则通过Handler把消息发送到消息队列,UI主线程会异步地从消息队列中取出消息并执行相应操作,比如 start、stop、pause 等。
class ActivityManagerService extends ActivityManagerNative abstract class ActivityManagerNative extends Binder implements IActivityManager
public final void attachApplication(IApplicationThread thread) { synchronized (this) { int callingPid = Binder.getCallingPid(); final long origId = Binder.clearCallingIdentity(); attachApplicationLocked(thread, callingPid); //调用 attachApplicationLocked Binder.restoreCallingIdentity(origId); } }
private void handleBindApplication(AppBindData data) { // ... // Allow disk access during application and provider setup. This could // block processing ordered broadcasts, but later processing would // probably end up doing the same disk access. final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites(); try { // If the app is being launched for full backup or restore, bring it up in // a restricted environment with the base application class. Application app = data.info.makeApplication(data.restrictedBackupMode, null); //创建一个Application对象 mInitialApplication = app;
// don't bring up providers in restricted mode; they may depend on the // app's custom Application class if (!data.restrictedBackupMode) { List<ProviderInfo> providers = data.providers; if (providers != null) { installContentProviders(app, providers); // For process that contains content providers, we want to // ensure that the JIT is enabled "at some point". mH.sendEmptyMessageDelayed(H.ENABLE_JIT, 10*1000); } }
// Do this after providers, since instrumentation tests generally start their // test thread at this point, and we don't want that racing. try { mInstrumentation.onCreate(data.instrumentationArgs); } catch (Exception e) { throw new RuntimeException( "Exception thrown in onCreate() of " + data.instrumentationName + ": " + e.toString(), e); }
if (instrumentation != null) { //应为上面传过来的参数为null,所以不会执行下面的代码 try { instrumentation.callApplicationOnCreate(app); } catch (Exception e) { if (!instrumentation.onException(app, e)) { throw new RuntimeException( "Unable to create application " + app.getClass().getName() + ": " + e.toString(), e); } } } return app; }
终于找到了Application的onCreate方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * Perform calling of the application's {@link Application#onCreate} * method. The default implementation simply calls through to that method. * * <p>Note: This method will be called immediately after {@link #onCreate(Bundle)}. * Often instrumentation tests start their test thread in onCreate(); you * need to be careful of races between these. (Well between it and * everything else, but let's start here.) * * @param app The application being created. */ public void callApplicationOnCreate(Application app) { app.onCreate(); }