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

Intent 顯式與隱式:Android 頁面跳轉的兩種思路

編輯:關於 Android 程式設計

Intent 是 Android 元件之間溝通的手段,而它啟動元件的方式分成兩類。理解這兩者的差別,不只是為了通過面試,更因為選錯會直接導致 ActivityNotFoundException。

顯式 Intent:明確指名要開哪一個

顯式 Intent 的特性是必須明確知道目標元件的 package 與 class 名稱。你直接告訴系統「開這個」,系統就照辦,不做任何比對。適用的情境是自己 App 內部的頁面跳轉,因為那些 Activity 的名字你都掌握在手裡。

隱式 Intent:只描述想做什麼

隱式 Intent 不需要知道目標是誰,只要描述動作(action)與資料(data),系統會自己去比對所有已安裝的 App,找出能處理這個請求的元件。它的價值在於跨應用協作——你想打開一張照片、打電話、分享文字,都不必知道對方是誰。

準備工作:在被跳轉的 Activity 中宣告 intent-filter

以 A 跳到 B 為例,B 若打算接受隱式 Intent,就必須在 Manifest 中加上 intent-filter:

<activity
    android:name=".DemoActivity"
    android:label="@string/demoActivity" >
    <intent-filter>
        <!-- 隱式 Intent 要匹配的 action,名稱自訂,可有多項 -->
        <action android:name="com.itcast.intent.DemoActivity" />

        <!-- category 說明元件類型,可自訂;不寫時呼叫端須對應 -->
        <category android:name="android.intent.category.DEFAULT" />

        <!-- 若指定 data,呼叫時就必須傳入相符的資料與 scheme -->
        <data
            android:host="cn.itcast.demo"
            android:scheme="itcast" />
    </intent-filter>
</activity>

這裡有個容易踩的規則:Intent 與 intent-filter 的匹配要求 action、category、data 三者都相符。少一項就找不到目標,而錯誤訊息只會說「找不到相符的 Activity」,不會告訴你少了哪一項,除錯時頗耗時間。

呼叫端:A 的版面與事件

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="第一個 activity" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="open"
    android:text="跳轉" />

接著在同一個方法中示範各種寫法:

public void open(View view) {

    /* ---------- 以下三種皆為顯式 Intent ---------- */

    // 方法一:指定 className(需含完整包名)
    Intent intent1 = new Intent();
    intent1.setClassName("com.itcast.intent", "com.itcast.intent.DemoActivity");
    // startActivity(intent1);

    // 方法二:建構子直接帶入 Context 與目標 class(最常用)
    Intent intent2 = new Intent(this, DemoActivity.class);
    // startActivity(intent2);

    // 方法三:透過 ComponentName 指定
    Intent intent3 = new Intent();
    ComponentName component = new ComponentName(
                           "com.itcast.intent", "com.itcast.intent.DemoActivity");
    intent3.setComponent(component);
    // startActivity(intent3);

    /* ---------- 隱式 Intent ---------- */

    Intent intent = new Intent();
    intent.setAction("com.itcast.intent.DemoActivity");
    // 無論 Manifest 中是否宣告 category,這裡都建議補上,
    // 否則部分系統會以 DEFAULT 之外的值比對而找不到目標
    intent.addCategory("android.intent.category.DEFAULT");

    // 若宣告了 data,呼叫時就必須傳入完全相符的值
    intent.setData(Uri.parse("itcast://cn.itcast.demo"));
    // startActivity(intent);

    /* ---------- 用系統內建的隱式 Intent ---------- */

    Intent imageIntent = new Intent(Intent.ACTION_PICK);
    imageIntent.setType("image/*");   // 指定資料類型
    startActivity(imageIntent);
}

B 這邊什麼都不用做,只要繼承 Activity 並重寫 onCreate() 即可。這個範例的重點是把跳轉的幾種寫法放在一起對照。

該用哪一種?

自己 App 內的頁面跳轉,一律用顯式。寫成 new Intent(this, TargetActivity.class) 最清楚,編譯器能幫你檢查 class 是否存在,重構時也有 IDE 支援。隱式 Intent 在這種情境下只會帶來「不小心被其他 App 攔截」的風險。

跨應用、或刻意讓使用者選擇,才用隱式。例如打開相簿挑圖、撥打電話、分享內容到社群。這時你不該也不需知道對方是誰。

兩個實務上的保護措施

隱式 Intent 前先確認有人接得住。由於目標由系統決定,很可能一台裝置上根本沒有能處理的 App,此時 startActivity() 會拋出 ActivityNotFoundException 並讓程式崩潰。建議先查詢:

if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent);
} else {
    // 提示使用者沒有適用的應用程式
}

自訂 action 前先想清楚。像範例中的 com.itcast.intent.DemoActivity 這種自訂 action 名稱,只有在「自己的 App 內以隱式方式啟動自己的元件」時才有意義。若想讓其他 App 也能呼叫,應該定義在公開文件中並加上版本控管,否則一旦改名,所有依賴方的整合都會當場失效。

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