关于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了.从哪里定义它是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是整个应用程序的入口点.而category中的android.intent.category.LAUNCHER意思是把这个Activityg归属到加载器类,即把这个Activity标注为自动会加载和启动的Activity,这样程序启动时候就先加载这个Activity了.参考手册上是这么说的----"the LAUNCHER category says that this entry point should be listed in the application launcher."意思和我理解的有出入.不过意思都是说这个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 }