android应用程序的入口点

时间:2012-04-07 19:06:10

标签: android mobile dalvik

当我们开发Android应用程序时,我们总是从主要活动的onCreate()方法开始。很明显,在调用onCreate()之前应该进行一些初始化。我的问题是:Android应用程序的入口点(或main方法)是什么? Dalvik VM在一开始就调用了什么(即,当它完成自己的初始化,并且即将控制转移到应用程序时)?我在哪里可以找到此main的代码?

4 个答案:

答案 0 :(得分:7)

第一个“入口”点是金士顿指出的应用程序类。

然而,获得第一个起点的最简单方法是在调试onCreate时检查堆栈。

您可以检查仪器,这听起来有点像您想要的。

http://developer.android.com/reference/android/app/Instrumentation.html

MainActivity.onCreate(Bundle) line: 12  
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1047   
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2627  
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2679   
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 
ActivityThread$H.handleMessage(Message) line: 2033  
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 4627    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 521  
ZygoteInit$MethodAndArgsCaller.run() line: 868  
ZygoteInit.main(String[]) line: 626 
NativeStart.main(String[]) line: not available [native method]  

答案 1 :(得分:6)

您应该扩展Application类并覆盖onCreate方法。

供参考:Application class

答案 2 :(得分:1)

我自己不知道,但这听起来很有趣。 This is the code会触发新的Activity并按照代码进行操作end up in JNI code

public void startActivityForResult(Intent intent, int requestCode) {
        if (mParent == null) {
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode);
            if (ar != null) {
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            }
            if (requestCode >= 0) {
                // If this start is requesting a result, we can avoid making
                // the activity visible until the result is received.  Setting
                // this code during onCreate(Bundle savedInstanceState) or onResume() will keep the
                // activity hidden during this time, to avoid flickering.
                // This can only be done when a result is requested because
                // that guarantees we will get information back when the
                // activity is finished, no matter what happens to it.
                mStartedActivity = true;
            }
        } else {
            mParent.startActivityFromChild(this, intent, requestCode);
        }
    }

Android源代码可用,但获取它有点棘手,因为它的文档记录很差。您将拥有to install repo,然后下载framework/base项目

答案 3 :(得分:0)

在核心Java程序中,我们需要一个main()方法,因为在执行字节代码时,JVM(Java虚拟机)将在类中搜索main()方法并开始在那里执行。

在Android中,(DVM)Dalvik虚拟机旨在查找一个类,该类是Activity的子类,并且设置为LAUNCHER以从其onCreate()方法开始执行应用程序,因此没有需要一个main()方法。