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

Intent 的基本用法:顯式啟動、隱式啟動與 Activity 的關閉時機

編輯:關於 Android 程式設計

一、Intent 是什麼角色

Android 應用通常包含多個 Activity。在當前 Activity 中呼叫 StartActivity 並傳入一個 Intent 物件,就能完成畫面切換與資料傳遞。

依 Intent 的建構方式,啟動可分為兩類:

  • 顯式啟動:在 Intent 中明確指定要啟動哪一個 Activity。
  • 隱式啟動:由系統依 Intent 的 action 與 data 自行決定啟動哪個 Activity。

二、顯式啟動

建立 Intent 時在建構子裡指明目標元件:

var intent = new Intent(this, typeof(Activity1));
StartActivity(intent);

這種寫法沒有其他匹配機制,就是直接點名,行為完全可控。

三、隱式啟動

隱式啟動不在 Intent 裡指名,而是交給系統依「動作 + 資料」去匹配。被匹配上的 Activity 可能來__).自你的 App,也可能是系統內建,甚至第三方應用。這種方式更能體現 Android 元件的復用性。

如果你的 App 需要打電話、寄信、傳訊息,或想借用其他程式來處理某種資料,優__,先考慮呼叫系統既有功能——也就是隱式啟動。只需宣告動作,剩下的交給系統:

var call = new Intent(Intent.ActionCall);
call.SetData(Android.Net.Uri.Parse("tel:13811112222"));
StartActivity(call);

以下是常用的 action 常數:

用 C# 表示這些常數時,輸入 Intent. 之後從提示清單中挑選即可。命名規則是把原本以底線分隔的單字改為每段首字母大寫,例如 Intent.ActionCall。

除了 action,還可以同時指定 Uri。此時系統會呼叫內建瀏覽器來處理:

Intent intent = new Intent(Intent.ActionView, Uri.Parse("https://www.google.com"));
StartActivity(intent);

四、關閉 Activity

呼叫 Finish() 可結束當前 Activity;呼叫 FinishActivity() 則可以結束先前由你啟動的某個 Activity。

但請記住:顯式關閉只該用在你確定「絕對不希望使用者再回到這個畫面」的情況。絕大多數場景下,都不應該主動呼叫 Finish,而該讓系統去管理 Activity 的生命週期。

五、實作範例

這個範例示範兩件事:透過 Intent 撥打電話,以及跳轉到另一個 Activity。專案需要 CALL_PHONE 權限。

先在模擬器中新增一些聯)>,人與電話號碼,再進行測試。左圖為主畫面,右圖是點擊「打電話」之後的撥號畫面。

點擊「轉到活動 1」之後的畫面:

1. 加入 CALL_PHONE 權限

在方案總管中雙擊專案的「Properties」,勾選 CALL_PHONE:

設定完成後,系統會自動在 AndroidManifest.xml 中寫入:

<uses-permission android:name="android.permission.CALL_PHONE" />

2. 主畫面版面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btn1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="打電話" />
    <Button
        android:id="@+id/btn2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="轉到活動 1" />
</LinearLayout>

3. 第二個畫面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:text="這是活動 1 的畫面\n\n提示:按【Back】按鈕返回"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_margin="30dp" />
</LinearLayout>

4. 主 Activity

[Activity(Label = "【例 12-1】Intent 基本用法 1")]
public class ch1201IntentDemoMain : Activity {
    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.ch1201_Main);

        // 隱式啟動:交給系統的撥號功能
        FindViewById<Button>(Resource.Id.btn1).Click += delegate {
            var call = new Intent(Intent.ActionCall);
            call.SetData(Android.Net.Uri.Parse("tel:13811112222"));
            StartActivity(call);
        };

        // 顯式啟動:明確指定目標 Activity
        FindViewById<Button>(Resource.Id.btn2).Click += delegate {
            var intent = new Intent(this, typeof(ch1201IntentDemoActivity1));
            StartActivity(intent);
        };
    }
}

5. 被啟動的 Activity

[Activity(Label = "【例 12-1】Intent 基本用法 1")]
public class ch1201IntentDemoActivity1 : Activity {
    protected override void OnCreate(Bundle savedInstanceState) {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.ch1201_Layout1);
    }
}

兩個按鈕放在一起對比,正好把顯式與隱式的差異說清楚了:前者你在程式碼裡指定目的地,後者你只描述「想做什麼」,由系統挑選合適的執行者。實務上,凡是系統已有成熟功能的場景(撥號、開瀏覽器、選圖片、分享),都該優先走隱式路線。

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