Android重要的原因

歡迎訪問本人博客!http://blog.csdn.net/ktb2007
自從 Android 和最初的 SDK 發佈以來,計算機技術新聞界一直非常關注 Android。Android 之所以重要主要有兩個原因。
Android 是 Google 進軍移動市場的主要行動。移動應用程序領域競爭激烈,新的競爭者很難涉足。Google 擁有大量資源和強大的品牌實力,有能力涉足任何市場。Google 進軍移動市場已經好幾年了。Android 原來屬於另一家公司,Google 通過收購這家公司大大提高了在移動市場上的競爭力。Google 的任何行動都會受到關注,而且引入新平臺也很引人注目。Android 同時具備這兩個因素。
第二個原因是 Android 的應用程序模型與衆不同。Android 應用程序並不是需要大量單擊操作的純粹的菜單式應用程序。Android 應用程序中確實有菜單和按鈕,但是 Android 在體系結構中引入了一種新穎的設計元素 intent。

intent
intent 是一種構造,應用程序可以通過它發出請求,這就像是發出求助信號。intent 可能像下面這樣:
"Wanted: An application to help me look up a contact" 或 "Wanted: An application to help me display this image" 或 "Wanted: An application to perform this geographic-based search"
應用程序可以按照相似或互補的方式進行註冊,表明它們有能力或有興趣執行各種請求或 intent。比如:
"Available: Application ready and willing to present contact records in clear, concise manner" 或 "Available: Application ready and willing to perform a geographic search"
這些是 IntentFilter 的示例,下面將要討論。

IntentFilter
應用程序通過一個稱爲 IntentFilter 的構造聲明它們能夠執行某些類型的操作。IntentFilter 可以在運行時進行註冊,也可以在 AndroidManifest.xml 文件中設置。下面的代碼片段取自一個對 SMS(文本)消息進行響應的 Android 應用程序:

清單 1. 對 SMS 進行響應的 Android 應用程序
                    
       <receiver class=".MySMSMailBox" >
            <intent-filter>
           <action android:value="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
     </receiver>


簡要介紹 intent 和 IntentFilter 之後,下一節介紹 Android 應用程序的四種主要類型。

Android 應用程序 — 概述
我們來看看 Android 應用程序的四種主要類型:活動、服務、接收器和 ContentProvider。我們還要看看顯示用戶界面(UI)元素的視圖。
活動
活動是最常用的 Android 應用程序形式。活動在一個稱爲視圖 的類的幫助下,爲應用程序提供 UI。視圖類實現各種 UI 元素,比如文本框、標籤、按鈕和計算平臺上常見的其他 UI 元素。
一個應用程序可以包含一個或多個活動。這些活動通常與應用程序中的屏幕形成一對一關係。
應用程序通過調用 startActivity() 或 startSubActivity() 方法從一個活動轉移到另一個活動。如果應用程序只需 “切換” 到新的活動,就應該使用前一個方法。如果需要異步的調用/響應模式,就使用後一個方法。在這兩種情況下,都需要通過方法的參數傳遞一個 intent。
由操作系統負責決定哪個活動最適合滿足指定的 intent。
服務和接收器
與其他多任務計算環境一樣,“在後臺” 運行着一些應用程序,它們執行各種任務。Android 把這種應用程序稱爲 “服務”。服務是沒有 UI 的 Android 應用程序。
接收器是一個應用程序組件,它接收請求並處理 intent。與服務一樣,接收器在一般情況下也沒有 UI 元素。接收器通常在 AndroidManifest.xml 文件中註冊。清單 2 是接收器代碼的示例。注意,接收器的類屬性是負責實現這個接收器的 Java 類。

清單 2. 接收器代碼
                    
package com.msi.samplereceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
public class myreceiver extends IntentReceiver
{
public void onReceiveIntent(Context arg0, Intent arg1)
{
  // do something when this method is invoked.
}
}

用 ContentProvider 進行數據管理
ContentProvider 是 Android 的數據存儲抽象機制。我們以移動設備上常見的一種數據爲例:地址簿或聯繫人數據庫。地址簿包含所有聯繫人及其電話號碼,用戶在使用手機時 可能需要使用這些數據。ContentProvider 對數據存儲的訪問方法進行抽象。ContentProvider 在許多方面起到數據庫服務器的作用。對數據存儲中數據的讀寫操作應該通過適當的 ContentProvider 傳遞,而不是直接訪問文件或數據庫。可能還有 ContentProvider 的 “客戶機” 和 “實現”。
下一節介紹 Android 視圖,這是 Android 在移動設備屏幕上顯示 UI 元素的機制。

視圖
Android 活動通過視圖顯示 UI 元素。視圖採用以下佈局設計之一:
LinearVertical
後續的每個元素都排在前一個元素下面,形成一個單一列。
LinearHorizontal
後續的每個元素都排在前一個元素右邊,形成一個單一行。
Relative
後續的每個元素相對於前一個元素有一定的偏移量。
Table
與 HTML 表相似的一系列行和列。每個單元格可以包含一個視圖元素。
選擇一種佈局(或佈局的組合)之後,就可以用各個視圖顯示 UI。
視圖元素由大家熟悉的 UI 元素組成,包括:
Button
ImageButton
EditText
TextView(與標籤相似)
CheckBox
Radio Button
Gallery 和 ImageSwitcher(用來顯示多個圖像)
List
Grid
DatePicker
TimePicker
Spinner(與組合框相似)
AutoComplete(具有文本自動補全特性的 EditText)
視圖是在一個 XML 文件中定義的。清單 3 給出一個簡單的 LinearVertical 佈局示例。

清單 3. 簡單的 LinearVertical 佈局
                    
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    androidrientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Activity 1!"
    />
<TextView  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Activity 1, second text view!"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Switch To Activity 2"
id="@+id/switchto2"
/>   
</LinearLayout>


注意,每個元素有一個或多個屬於 Android 名稱空間的屬性。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章