Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發筆記:核心元件與資料傳遞入門

編輯:關於 Android 程式設計

在 Android 中,每一個應用程式都運行於自己的進程裡,並擁有一個獨立的 Dalvik 虛擬機實例,這樣可以防止某個程式崩潰時影響到其他程式。值得一提的是,Dalvik 虛擬機是基於寄存器的,而傳統的 JVM 則是基於堆疊的。

應用程式的六大核心元件

1. Activity:使用者所能看到的畫面,負責處理應用的整體性工作,例如監聽系統事件、顯示指定的 View、啟動其他 Activity 等。

2. Intent:負責在 Activity 之間切換。Intent 描述應用想做什麼,是應用程式的「意圖」,其資料結構包含動作與動作對應的資料(以 URI 表示)。與之對應的是 IntentFilter,用來描述某個 Activity 或 IntentReceiver 能處理哪些 Intent,需要在 AndroidManifest.xml 中宣告。呼叫 startActivity(intent) 即可實現切換:

[java]
Intent showNextPage_intent = new Intent();
showNextPage_intent.setClass(this, NextPageActivity.class);
startActivity(showNextPage_intent);

3. IntentReceiver:用於響應外部事件,可以在 AndroidManifest.xml 或透過 Context.registerReceiver() 註冊。

4. Service:具有生命週期的一種服務。使用 Context.startService() 可啟動服務並在後台運行;使用 Context.bindService() 則綁定到一個服務,透過服務提供的介面與之通信,例如播放器的暫停、重播等操作。

其生命週期有三種情況:
    1> 以 startService 啟動、以 stopService 結束。
    2> 與 bindService 進程同生共死,或呼叫 unbindService 結束。
    3> 混合使用時,需要 stopService 與 unbindService 都呼叫後,Service 才會終止。

[java]
// Manifest.xml 中的 Service 定義
<service android:name=".Music">
  <intent-filter>
    <action android:name="com.liangshan.wuyong.START_AUDIO_SERVICE" />
    <category android:name="android.intent.category.default" />
  </intent-filter>
</service>
// Service 子類中的播放邏輯
public void onStart(Intent intent, int startId) {
  super.onStart(intent, startId);
  player = MediaPlayer.create(this, R.raw.seven_days);
  player.start();
}
public void onDestroy() {
  super.onDestroy();
  player.stop();
}
// Activity 中透過 Intent 開啟、關閉 Service
startService(new Intent("com.gruden.sy.START_AUDIO_SERVICE"));
stopService(new Intent("com.gruden.sy.START_AUDIO_SERVICE"));

5. ContentProvider:資料共享機制,是為了解決不同應用包或程式之間的資料共享問題。

6. Bundle:用於在 Activity 之間傳輸資料,與 Activity、Intent 關係密切。

Activity 間的資料傳遞

第一種方式是把資料寫進 Bundle,再透過 Intent 傳遞:

[java]
// 資料寫入 Intent
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("key_Name", "NAME");
bundle.putString("Key_Salary", Salary);
intent.putExtras(bundle);
intent.setClass(this, NewActivity.class);
startActivity(intent);

[java]
// 從 Intent 中取得資料
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("Key_Name");
String salary = bundle.getString("Key_Salary");
setTitle(name + '-' + salary);

第二種方式是透過 startActivityForResult,在來源 Activity 中重寫 onActivityResult 方法等待 Intent 返回結果:

[java]
startActivityForResult(intent, REQUEST_ASK);// 開啟 Intent 時同時帶上請求碼

// 重寫 onActivityResult()
void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_ASK){
        if(resultCode == RESULT_CANCELED){
             setTitle("cancel...");
        }else if(resultCode == RESULT_OK){
             Bundle showBundle = data.getExtras();
             String Name = showBundle.getString("myName");
        }
    }
}

目標 Activity 則透過 setResult 把結果碼與資料綁進 Bundle,再經由 Intent 傳回請求方:

[java]
bundle.putString("Name", NAME);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);// 透過 Intent 返回結果
finish();

掌握這些核心元件與資料傳遞方式,就掌握了 Android 開發最基本的骨架。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved