編輯:關於 Android 程式設計
Intent 是 Android 元件之間溝通的手段,而它啟動元件的方式分成兩類。理解這兩者的差別,不只是為了通過面試,更因為選錯會直接導致 ActivityNotFoundException。
顯式 Intent 的特性是必須明確知道目標元件的 package 與 class 名稱。你直接告訴系統「開這個」,系統就照辦,不做任何比對。適用的情境是自己 App 內部的頁面跳轉,因為那些 Activity 的名字你都掌握在手裡。
隱式 Intent 不需要知道目標是誰,只要描述動作(action)與資料(data),系統會自己去比對所有已安裝的 App,找出能處理這個請求的元件。它的價值在於跨應用協作——你想打開一張照片、打電話、分享文字,都不必知道對方是誰。
以 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」,不會告訴你少了哪一項,除錯時頗耗時間。
<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 也能呼叫,應該定義在公開文件中並加上版本控管,否則一旦改名,所有依賴方的整合都會當場失效。
Android Camera TakePicture 拍照過程源碼分析
承接上一篇,本文繼續講解 Camera 拍照等具體功能的實作流程。Camera 子系統採用 C/S 架構,客戶端與服務端位於兩個不同的進程,它們透過 Android 的
Android 4.4 Browser 瀏覽器模式選項默認值修改指南
在定製 Android 4.4 系統時,往往需要修改內置 Browser 瀏覽器的默認「瀏覽器模式」(即 User-Agent 模式)。實現方法很簡單,只需定位並修改對應
Android 中 sp 與 dp 的差異與使用時機
許多 Android 開發者在剛開始學習時,都會被告知尺寸使用 dp、字體使用 sp,並將數值放入 dimens.xml。然而,若把字體大小寫成 dp,畫面看似也能正常顯
Cordova 5.3.1(PhoneGap)混合開發環境搭建完整指南
引言:認識 Cordova先簡單介紹 Cordova 的來歷。Cordova 的前身叫 PhoneGap,自被 Adobe 收購後交由 Apache 管理,並將核心功能開