Android程式的入口點
android應用程式,由一到多個Activity組成.每個Activity沒有很緊密的聯絡,因為我們可以在自己的程式中呼叫其它Activity,特別是呼叫自己的程式碼之外生成的Activity,比如android提供的發簡訊或者打電話的Activity.
Intent call = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phonenumber);
startActivity(call);
Intent sms = new Intent(Intent.ACTION_SENDTO,Uri.parse("smsto:"+phonenumber);
startActivity(sms);
從這點上看,android應用程式實際上是由多個Activity按照一定的次序拼裝起來的, 只不過拼裝的過程中,後臺傳遞了一些資料,使得各個Activity之間能比較好的銜接起來.
在 android應用程式中,並沒有像c++和java這樣有main函式來作為應用程式的入口.android應用程式提供的是入口Activity,而非入口函式.
在eclipse中建立一個android應用程式的時候,預設會建立一個Activity. 這個Activity實際上就是入口Activity了.AndroidManifest.xml檔案中定義了整個android應用所包含的Activity.
預設生成的Activity的定義為
<activity android:name=".activity01" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
action節點中的android.intent.action.MAIN表明它所在的Activity是整個應用程式的入口點
我們可以對比下eclipse中console端輸出的日誌來看下.初始,我把 <category android:name="android.intent.category.LAUNCHER" />這一行在原先的xml檔案中註釋掉了.可以看到控制檯會報錯"No Launcher activity found!",實際上此時在虛擬裝置上的應用程式列表中找不到"lifecycle_test"這個app,而且也沒法執行,根本載入不起來.後面把註釋去掉,還原到最初始的狀態,然後在啟動應用,可以看到"No Launcher activity found!"這部分報錯資訊沒有了,而且虛擬裝置的螢幕上,可以看到"lifecycle_test"這個app已經成功運行了.
[2011-08-11 09:29:10 - lifecycle_test] ------------------------------
[2011-08-11 09:29:10 - lifecycle_test] Android Launch!
[2011-08-11 09:29:10 - lifecycle_test] adb is running normally.
[2011-08-11 09:29:10 - lifecycle_test] No Launcher activity found!
[2011-08-11 09:29:10 - lifecycle_test] The launch will only sync the application package on the device!
[2011-08-11 09:29:10 - lifecycle_test] Performing sync
[2011-08-11 09:29:10 - lifecycle_test] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'myavd_2.2'
[2011-08-11 09:29:15 - lifecycle_test] Application already deployed. No need to reinstall.
[2011-08-11 09:29:15 - lifecycle_test] \lifecycle_test\bin\lifecycle_test.apk installed on device
[2011-08-11 09:29:15 - lifecycle_test] Done!
[2011-08-11 09:30:47 - lifecycle_test] ------------------------------
[2011-08-11 09:30:47 - lifecycle_test] Android Launch!
[2011-08-11 09:30:47 - lifecycle_test] adb is running normally.
[2011-08-11 09:30:47 - lifecycle_test] Performing test.activity.leipei.activity01 activity launch
[2011-08-11 09:30:47 - lifecycle_test] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'myavd_2.2'
[2011-08-11 09:30:47 - lifecycle_test] Uploading lifecycle_test.apk onto device 'emulator-5554'
[2011-08-11 09:30:47 - lifecycle_test] Installing lifecycle_test.apk...
[2011-08-11 09:30:54 - lifecycle_test] Success!
[2011-08-11 09:30:55 - lifecycle_test] Starting activity test.activity.leipei.activity01 on device emulator-5554
[2011-08-11 09:30:57 - lifecycle_test] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=test.activity.leipei/.activity01 }