android學習11"/>
android學習1
1 安裝
- google為android開發准備了方便的環境,我的系統是debian6.0 stable,軟件比較老, 在安裝android開發環境前,還擔心系統不支持,其實擔心是多余的。
- 首先安裝java,debian系統自帶的java6.0夠用了,ADT(Android Developer Tools),地 址是:http://developer.android.com/sdk/index.html,挑選適合自己的版本吧,我選 的是adt-bundle-linux-x86_64.zip,下載需要時間,耐心等待:),然后解壓,我解壓 到/opt/下,運行/opt/adt-bundle-linux/eclipse/eclipse,ok,成功。安裝的 andorid版本是4.2,目前還沒有android手機,先用軟件模擬吧。
2 第一個程序
今天建一個最簡單的程序hello world。
- 建立項目,點擊File->New->Other,選擇Android Application Project,在 Application Name處填寫:HelloAndroid,剩下的默認選擇,下一步,Configure Project,也選擇默認,下一步,Configure Launcher Icon,下一步,Create Activity,選擇第一項:BlankActivity,下一步,New Blank Activity,輸入 "Activity Name", "Layout Name",保持不變,點擊Finish,程序建立成功。程序進入 后,顯示activity_main.xml設計的主界面,,我們暫時用默認界面。
- 運行一下,點擊Run->Run As->Android Application,用模擬機運行。
- 模擬比較慢,Console中顯示模擬過程,稍等一下,一個完整的Andorid手機顯示出來 了,基本框架都有,你可以操縱試試,我們編寫的程序HelloAndroid也出現apps管理中。
[2012-11-23 10:57:05 - HelloAndroid] ------------------------------ [2012-11-23 10:57:05 - HelloAndroid] Android Launch! [2012-11-23 10:57:05 - HelloAndroid] adb is running normally. [2012-11-23 10:57:05 - HelloAndroid] Performing com.example.helloandroid.MainActivity activity launch [2012-11-23 10:57:10 - HelloAndroid] Launching a new emulator with Virtual Device 'AVD_for_3_2in_HVGA_slider_ADP1' [2012-11-23 10:57:10 - Emulator] waitpid(): No child processes [2012-11-23 10:57:10 - Emulator] Failed to create Context 0x3005 [2012-11-23 10:57:10 - Emulator] emulator: WARNING: Could not initialize OpenglES emulation, using software renderer. [2012-11-23 10:57:11 - HelloAndroid] New emulator found: emulator-5554 [2012-11-23 10:57:11 - HelloAndroid] Waiting for HOME ('android.process.acore') to be launched... [2012-11-23 10:57:51 - HelloAndroid] HOME is up on device 'emulator-5554' [2012-11-23 10:57:51 - HelloAndroid] Uploading HelloAndroid.apk onto device 'emulator-5554' [2012-11-23 10:57:51 - HelloAndroid] Installing HelloAndroid.apk...
3 程序分析
- 第一個程序運行成功,我們現在分析一下。
- 程序包結構是:com.example.helloandroid,包內只有一個類:MainActivity,我們先看看:
package com.example.helloandroid; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
- MainActivity.java繼承自Activity,內部只有兩個函數,均是覆蓋Activity的。每一個 Android程序必須繼承Activity或其子類,有關Activity生命周期相關的有以下主要函 數,這些函數交給系統調研,今天Activity就了解這些,細節以后再分析。
protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy();
- MainActivity.java程序很簡單,程序建立時,onCreate中加載 R.layout.activity_main資源,onCreateOptionsMenu加載R.menu.activitymain資源, 這兩個資源都是int類型,定義在R.java類中,R類不要手動編輯,系統會根據res設置自 動修改,我們看看R.java定義:
package com.example.helloandroid; public final class R { public static final class attr { } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class id { public static final int menu_settings=0x7f070000; } public static final class layout { public static final int activity_main=0x7f030000; } public static final class menu { public static final int activity_main=0x7f060000; } public static final class string { public static final int app_name=0x7f040000; public static final int hello_world=0x7f040001; public static final int menu_settings=0x7f040002; } public static final class style { /** Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. Base application theme for API 11+. This theme completely replaces AppBaseTheme from res/values/styles.xml on API 11+ devices. API 11 theme customizations can go here. Base application theme for API 14+. This theme completely replaces AppBaseTheme from BOTH res/values/styles.xml and res/values-v11/styles.xml on API 14+ devices. API 14 theme customizations can go here. */ public static final int AppBaseTheme=0x7f050000; /** Application theme. All customizations that are NOT specific to a particular API-level can go here. */ public static final int AppTheme=0x7f050001; } }
- layout.activity_main和menu.activity_main均是int類型,分別對應工程資源列表 res下layout的activity_main.xml,menu的activity_main.xml。 layout.activity_main.xml中定義了TextView,text是 @string/helloworld,menu.activity_main.xml中定義了item,title是 @string/menusettings,這兩個都是字符串,定義在res下values的 strings.xml,strings.xml定義了三個字符串,程序運行后,主界面顯示一個TextView 控件,內容是strings.xml中的helloworld(Hello world!),菜單只有一個item,名字是strings.xml 中的menusettings(Settings),
- layout.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
- menu.activity_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <itemy android:id="@+id/menu_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/menu_settings"/> </menu>
- values.strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HelloAndroid</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> </resources>
3.1 修改一下
- 我們修改一下程序的values.strings.xml,看看和分析的是否一樣,將appname改 為:firest android,helloworld改為:the machine of awareness,menusetting改 為:first menu,再運行一下程序,程序名應該顯示firest android,界面 內容顯示the machine of awareness,菜單內容是first menu。
4 小結
- 從第一個程序可以看出,每一個Android程序都是從Activity繼承而來,Activity生命周 期由系統統一管理,並從onCreate啟動,程序的結構(layout),菜單(menu),數據 (values)均是配置在xml文件中,避免了硬編碼。