編輯:關於 Android 程式設計
Android 應用通常包含多個 Activity。在當前 Activity 中呼叫 StartActivity 並傳入一個 Intent 物件,就能完成畫面切換與資料傳遞。
依 Intent 的建構方式,啟動可分為兩類:
建立 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);
呼叫 Finish() 可結束當前 Activity;呼叫 FinishActivity() 則可以結束先前由你啟動的某個 Activity。
但請記住:顯式關閉只該用在你確定「絕對不希望使用者再回到這個畫面」的情況。絕大多數場景下,都不應該主動呼叫 Finish,而該讓系統去管理 Activity 的生命週期。
這個範例示範兩件事:透過 Intent 撥打電話,以及跳轉到另一個 Activity。專案需要 CALL_PHONE 權限。
先在模擬器中新增一些聯)>,人與電話號碼,再進行測試。左圖為主畫面,右圖是點擊「打電話」之後的撥號畫面。
點擊「轉到活動 1」之後的畫面:
在方案總管中雙擊專案的「Properties」,勾選 CALL_PHONE:
設定完成後,系統會自動在 AndroidManifest.xml 中寫入:
<uses-permission android:name="android.permission.CALL_PHONE" />
<?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>
<?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>
[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);
};
}
}
[Activity(Label = "【例 12-1】Intent 基本用法 1")]
public class ch1201IntentDemoActivity1 : Activity {
protected override void OnCreate(Bundle savedInstanceState) {
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.ch1201_Layout1);
}
}
兩個按鈕放在一起對比,正好把顯式與隱式的差異說清楚了:前者你在程式碼裡指定目的地,後者你只描述「想做什麼」,由系統挑選合適的執行者。實務上,凡是系統已有成熟功能的場景(撥號、開瀏覽器、選圖片、分享),都該優先走隱式路線。
Android BlueDroid(一):BlueDroid 藍牙協議棧入門
關鍵詞:bluedroid、bluez本文為系列文章的首篇,將帶大家走進 Android 的藍牙協議棧世界,並歡迎各方指正、共同交流進步。一、常見名詞速覽在正式展開之前,
來電管家實作(一):ActivityGroup 分頁界面設計
這幾天忙著寫來電管家這個小工具,主體功能基本完成,剩下後續打磨。(之前的記事本專案擱著沒動,但不會放棄。) 來電管家的核心就是攔截,具體拆成四項: 1. 黑名單 / 白名
SharedPreferences 的正確使用:App 設置界面的數據保存與讀取
歷經兩個多月的努力,項目即將在 11 月交付使用,基礎功能都已完成,只剩下系統設置界面還沒做完,如下圖: 這個界面的實現要點是:多個界面需要顯示大量數據,並依據某一項進
Android 自動掛斷電話的實作:透過 AIDL 呼叫 ITelephony
透過前面《AIDL 小結》與《AIDL 深入》兩篇文章,相信各位對 Android 的 AIDL 機制已經有了基本認識。這次我們就來動手實作一個實用的功能——自動掛斷電話