顯示具有 開發前須知 標籤的文章。 顯示所有文章
顯示具有 開發前須知 標籤的文章。 顯示所有文章

2011年6月13日 星期一

Android 開發前須知 - Activity, Intent, Content Provider, Service 簡介


Android 應用程式由四個主要模組所組成,此四個模組分別為 Activity、Intent、Content Provider、Service,雖然一個 Android 應用程式都由這四個模組構成,但並不代表著每個應用程式必須包含每個模組,像是簡單的印出 HelloWorld 字串的應用,只需要包含一個 Activity 即可達成。

Activity

Activity 是應用裡面最基本的模組,每一個 Activity 代表著一個螢幕,每一個螢幕所負責的就是處理應用程式所需的工作或是跳轉至其他 Activity 等功能,例如我們需要一個應用為取得課程資訊,這時可以設計成,第一個 Activity 顯示著所有學院的列表頁,第二個 Activity 顯示資訊系所所有課程列表,第三個 Activity 顯示計算機概論課程資訊,也因為如此的設計讓應用的功能上可以容易的轉換。


package nchu.test.HelloWorld;

import android.app.Activity;
import android.os.Bundle;

public class HelloWorld extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
}
}

所有的 Activity 都繼承於 android.app.Activity 類別,並且會馬上呼叫 onCreate() 函式,不過並不是呼叫 onCreate() 之後 Activity 就能馬上運行,實際上 Activity 要能開始運行需要經過三個過程為"建立 -> 開始 -> 重繪",三個過程相對應的函式如下。onResume()為重繪整個螢幕。

開始 -> onCreate() -> onStart() -> onResume() -> Activity 運行


Intent

當創建了許多 Activity 之後,該如何讓這些 Activity 切換,我們可以利用 Intent 這個類別。

Intent 類別主要是描述應用的功能,描述的結構為動作和動作相對應的資料,利用 IntentFilter 裡的描述可以透過比對找到應用程式需要呼叫的 Activity 是哪一個。

我們看一下 AndroidManfest.xml 檔案裡 IntentFilter 描述:

在 action android:name 這行描述的是 Activity 的動作,動作名稱為 android.intent.action.MAIN。

在category android:name 這行描述的是使用哪個種類的元件來掌控 Intent,這裡使用的是category.LAUNCHER,主要可以將 Activity 放置到 top-level application launcher 去執行。


<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Activity 的跳轉會經過以下幾個步驟:
首先暫停 Activity01,然後 Activity02 進入 Activity 的生命週期,之後先停止 Activity01 然後才將其摧毀。

Activity01 onPause() -> Activity02 onCreate() ->
Activity02 onStart() -> Activity02 onResume() ->
Activity01 onStop() -> Activity01 onDestroy()


Content Provider

一般來說,在 Android 應用程式裡是無法取得不同應用程式的資料,不過當我們使用 Android 設備時可以儲存通訊人連絡電話,不同的應用程式開啟都可以共同的存取這些通訊錄的資料,而這個資料共有的功能就是由 Content Provider 所提供。

Content Provider 是一個特殊的資料儲存類型,並且提供了一些標準的介面方便資料的存取,像是 query()、insert() 或是 update() 等,當我們在應用程式中有需要使用到 Content Provider 時,則必須在 AndroidManifest.xml 檔案中宣告。


Service

當我們開啟一個撥放歌曲的應用程式聽著歌,接著想要上網找尋資料,勢必會遇到一個狀況,必須跳離播歌的程式才能上網,當我們跳離開程式後又希望歌曲可以在背景中繼續撥放著,那這個部份就是由 Service 所提供。

Service 像是在背景提供著服務,不像 Activity 有著介面可以讓使用者操作,通常 Service 的生命周期都是比較長,屬於在背景長駐執行一些不中斷的程式。


相關連結:
Android Developer : Activity
Android Developer : Intents and Intent Filters
Android Developer : Content Provider
Android Developer : Services

2011年6月11日 星期六

Android 開發前須知 - 目錄結構


Android 預設的整體架構是以 MVC 的方式在做規劃,利用 MVC 的架構我們可以把運算邏輯、版面配置和控制元件獨立開來方便在開發過程中的修改更新,讓整個系統更有擴充的彈性,如果要利用到這些好處我們必須對其目錄結構有初步的認識。

在 Android 的開發目錄結構中有幾個較為重要分別是:
src, res, gen, R.java, AndroidManfest.xml

src目錄
專案的 package 和 source code 都會存放在此地


res -> values
專案的資源都可以在此定義,定義的格式為 xml 檔案,檔名可以利用資源類型做分類,像是利用 strings.xml 或是 colors.xml 我們可以很容易的判斷字串資源或是色彩資源,定義的方式為一組關於資源類型的標籤並且賦予其姓名,並且利用開始標籤與結束標籤將我們所需的值包覆。如果使用 Eclipse 作為開發環境,Google 提供了非常方便的編輯器,在編輯器可以直接利用 Add 或是 Remove 等按鍵產生需要的資源。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="white">#FFFFFF</drawable>
<drawable name="black">#000000</drawable>
</resources>



res -> layout -> main.xml
layout 裡面放置的是版面佈局的 xml 描述檔,當應用程式中所有需要用到的 UI 元件或是排版方式都必須在此檔案內做描述。如果使用 Eclipse 作為開發環境,Google 提供了非常方便的編輯器,可以利用拖拉以及點選參數填值的方式,輕鬆達到版面的佈置,而不需要進入xml檔案內做編寫。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/white" android:onClick="cvOnClick">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/KilogramsLabel" android:textColor="@drawable/black" android:text="@string/kgLabel"></TextView>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/kilograms" android:numeric="integer"></EditText>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ConverButton" android:text="@string/CvButton" android:onClick="cvOnClick"></Button>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/PondsLabel" android:text="@string/LbLabel" android:textColor="@drawable/black"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Ponds" android:textColor="@drawable/black"></TextView>
</TableRow>
</TableLayout>



gen -> R.java
此檔案為自動產生並且是唯讀檔,定義了所有資源的索引檔,假設當我們在 strings.xml 中加入了一個字串資源
<string name="hello">Hello World, HelloMyAndroid!</string>

當專案重整之後 R.java 將會自動的把放置 hello 這個名稱的資源位置給加入

/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/

package idv.tw.kudodir.hellomyandroid;

public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}


AndroidManfest.xml
當應用程式中有使用到任何的 Activity, Intent, Content Provider, Service 都必須在這個檔案內做描述。如果使用 Eclipse 作為開發環境,Google 提供了非常方便的編輯器,在編輯器內已經有許多預設好的欄位,只需要選擇欄位填入適當的值,就可以產生出。

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger