快速上手:Ophone及Android入門教程
下文是JavaEye的zhang_xzhi_xjtu總結的OPhone/Androind入門教程,小編感覺不錯,在此推薦給大家學習。由于OPhone本質上和Android幾乎沒什么兩樣,所以雖然本教程中所用的是OPhone,但實質上無異于一個Android入門教程。
本教程主要參考
http://code.google.com/p/androidbmi/wiki/DiveIntoAndroid
這個教程是一個比較好的教程,但是天下沒有人人滿意的東西。
主要而言,對某些讀者,該教程有兩個小小的不足,1是繁體字,2是作者照顧了很多剛入門的程序員,寫的太細致了,比如還要抽空講一講java,xml的一些語法。
本文的特點就是,1普通話,2面向有java,xml經驗的程序員。主要是從一個小例子闡述一下OPhone/Android的一些重要概念。
本文的目標是除去搭建環(huán)境外的一個5分鐘快速入門。并提供一個簡單的程序代碼可以用來修改驗證一些OPhone/Android入門時的想法,并對OPhone/Android的重要概念有一個大致的了解。
本文提供的code的測試搭建環(huán)境為OPhone。
1 背景
Android是什么?
Android 是Google開發(fā)的基于Linux平臺的開源手機操作系統(tǒng)。
OPhone是什么?
OPhone是中國移動OMS系統(tǒng)下定制的手機。OPhone上的程序和Android是兼容的。
Emulator是什么?
方便程序員開發(fā)其他設備上的程序,而對其他設備做的一個軟件模擬器。開發(fā)的程序可以在模擬器上部署,執(zhí)行,調試。
2 安裝開發(fā)環(huán)境
Android: http://code.google.com/p/androidbmi/wiki/InstallAndroid
JDK5+
Eclipse3.3+
ADT
Android SDK(包含Emulator)
OPhone: http://www.ophonesdn.com/documentation/ophone/gettingstarted/installing_sdk.html
JDK5+
Eclipse3.4.2
ADT
WDT
OPhoneSDK(包含Emulator)
注意安裝的***步驟在Eclipse中設置Android SDK Location。
3 創(chuàng)建簡單的OPhone程序
在Eclipse中創(chuàng)建一個Android project。
里面有如下選項需要填寫:
Project Name:項目名稱。
Package name:命名空間。
Activity name:先理解為程序的入口類,以后解釋。
Application name:程序名稱。
記得加入android.jar和oms.jar。
4 OPhone目錄結構及意義
創(chuàng)建好的OPhone項目有如下基本結構。
src:java code。
res:OPhone的資源。
res\drawable:圖片。
res\layout:布局描述。
res\values:字符串定義。
AndroidManifest.xml:描述該OPhone程序。
5 OPhone關鍵概念。
部署相關:
雖然我們是用java開發(fā)的,但是結果并不是常見的jar,而是dex,或者apk。
過程是這樣的,我們編寫java,編譯成class,優(yōu)化處理成dex(相對來說mobile還是一個受限環(huán)境),和資源一起打包成apk文件。部署后由Dalvik VM執(zhí)行。
一個Ophone的應用是多個Activity的集合。可以在AndroidManifest.xml中指定該Ophone App的入口Activity.
一個Activity是一個和用戶的基本交互流,有其自身的生命周期。Activity之間可以通過Intent傳遞信息。
關于Activity的生命周期可以參考
http://code.google.com/p/androidbmi/wiki/LifeCycle
一個Activity的實現(xiàn)采用的是MVC,其中code部分負責MC,res\layout中的xml負責View。
App中使用的字符串都可以放在res\values下xml中統(tǒng)一維護,方便系統(tǒng)的維護,管理。
項目自動生成一個R.java來管理資源相關的引用。開發(fā)者不需要手動修改該文件。
6 小例子截圖。
這個例子的功能是用戶輸入名字,程序對該用戶說hello。
7 關鍵代碼解釋
HelloOPhone定義了入口Activity。
Echo定義了打招呼的Activity。
看看HelloOPhone的View部分是怎么定義的,xml的自描述性真好,都不用解釋。
Xml代碼
- < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- < TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="@string/input_msg" />
- < EditText android:id="@+id/name" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="" />
- < Button android:id="@+id/ok" android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="@string/ok" />
- < /LinearLayout>
看看一個Activity是怎樣和這個xml聯(lián)系起來的。通過自動生成的R.java中的定義。注意定義的xml文件名改變R會自動刷新。
Java代碼
- setContentView(R.layout.input);
再看看一個Activity如何通過xml中的id找到該View.
Java代碼
- b_ok = (Button) findViewById(R.id.ok);
再看看Activity怎么通過Intent啟動另一個Echo Activity,并且給那個Activity傳遞信息。
Java代碼
- Intent intent = new Intent();
- intent.setClass(HelloOPhone.this, Echo.class);
- Bundle bundle = new Bundle();
- bundle.putString("name", name);
- intent.putExtras(bundle);
- startActivity(intent);
再看看Echo如何接收傳遞來的信息。
Java代碼
- Bundle bunde = this.getIntent().getExtras();
- String name = bunde.getString("name");
***簡單的看看AndroidManifest.xml如何描述該app。
其中指明了命名空間,程序的圖像等等。
同時指出該app由2個Activity組成,并設置了HelloOPhone為入口Activity。
Xml代碼
- < manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="allen.oms" android:versionCode="1" android:versionName="1.0.0">
- < application android:icon="@drawable/icon" android:label="@string/app_name">
- < activity android:name=".HelloOPhone" android:label="@string/app_name">
- < intent-filter>
- < action android:name="android.intent.action.MAIN" />
- < category android:name="android.intent.category.LAUNCHER" />
- < /intent-filter>
- < /activity>
- < activity android:name=".Echo" android:label="@string/app_name" />
- < /application>
- < /manifest>
OPhone/Androind入門教程到此結束,希望對大家有所幫助。
【編輯推薦】