編輯:Android 開發實例
按下撥號鍵到通話真正建立,中間其實跨越了好幾個層級:從 Contacts 應用、Phone 應用,一路往下走到 framework 的 telephony 層,最後由 RIL 把請求交給中繼 daemon。以下依序拆解這條路徑。
整個流程從 /packages/Contacts/src/com/android/contacts/ 下的 TwelveKeyDialer.java 開始:
dialButtonPressed() {
.....
final String number = mDigits.getText().toString();
startActivity(newDialNumberIntent(number));
mDigits.getText().clear();
finish();
}
其中 newDialNumberIntent() 的定義如下:
private Intent newDialNumberIntent(String number) {
final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED,
Uri.fromParts("tel", number, null));
.....
}
關鍵在於它送出的 Action 是 ACTION_CALL_PRIVILEGED。查 AndroidManifest 會發現對應的元件名為 PrivilegedOutgoingCallBroadcast,但在 packages/Phone/src/ 底下卻找不到這個檔案。原因是它在 manifest 中以 <activity-alias /> 的形式宣告:
<activity-alias /> 透過 android:targetActivity = OutgoingCallBroadcast 指向真正的實作
也就是說,PrivilegedOutgoingCallBroadcast 只是別名,真身是 OutgoingCallBroadcast。
資料流進到 OutgoingCallBroadcast.java 的 onCreate():
protected void onCreate(Bundle icicle) {
.....
Intent intent = getIntent();
.....
String action = intent.getAction();
.....
final boolean emergencyNum = (number != null)
&& PhoneNumUtils.isEmergencyNumber(number); // 是否為緊急號碼
.....
if (Intent.ACTION_CALL_PRIVILEGED.equals(action)) {
action = emergencyNum ? Intent.ACTION_CALL_EMERGENCY : Intent.ACTION_CALL;
intent.setAction(action);
}
.....
intent.setClass(this, InCallScreen.class);
startActivity(intent);
}
這裡做了兩件事:依號碼是否為緊急號碼把 Action 改寫成 ACTION_CALL_EMERGENCY 或 ACTION_CALL,然後把目標元件指向 InCallScreen。
InCallScreen.java 同樣位於 packages/Phone/src/com/android/phone。它的 onCreate() 會呼叫 initInCallScreen() 初始化通話介面,並透過 registerForPhoneStates() 註冊電話狀態監聽。
protected void onNewIntent(Intent intent) {
.....
String action = intent.getAction();
.....
else if (action.equals(Intent.ACTION_CALL)
|| action.equals(Intent.ACTION_CALL_EMERGENCY)) {
.....
InCallInitStatus status = placeCall(intent);
}
}
private InCallInitStatus placeCall(Intent intent) {
.....
int callStatus = PhoneUtils.placeCall(....);
}
PhoneUtils.java 也在同一目錄下:
public static int placeCall(...) {
Connection connection;
connection = PhoneApp.getInstance().mCM.dial(phone, numberToDial);
}
追進 PhoneApp.java 可以發現 mCM 是 CallManager 的物件,而 CallManager 屬於 frameworks 層——資料流自此進入 framework。
進入 /frameworks/base/telephony/java/com/android/internal/telephony:
public Connection dial(Phone phone, String dialNumber) throws CallStateException {
Phone basePhone = getPhoneBase(phone);
Connection result;
result = basePhone.dial(dialString);
........
}
private static Phone getPhoneBase(Phone phone) {
if (phone instanceof PhoneProxy) {
return phone.getForegroundCall().getPhone();
}
return phone;
}
由於 PhoneBase 抽象類實作了 Phone 介面,而 GSMPhone 又繼承了 PhoneBase,因此這裡的 phone.getForegroundCall() 實際上就是在 GSMPhone 物件上呼叫同名方法。
GSMPhone.java 位於 .../internal/telephony/gsm/:
GsmCallTracker mCT;
public GsmCall getForegroundCall() {
return mCT.foregroundCall;
}
GsmCallTracker.java 中宣告了 foregroundCall:
GSMCall foregroundCall = new GSMCall(this);
而 GSMCall.java 的 getPhone() 反過來指向擁有者:
GSMCallTracker owner;
public Phone getPhone() {
return owner.phone;
}
GSMCallTracker 裡正好有 GSMPhone phone; 的宣告。繞了一圈,結論是第五步中 getPhoneBase() 回傳的就是 GSMPhone 物件,而緊接著的 basePhone.dial() 呼叫的正是它的 dial 方法。
// GSMPhone.java
public Connection dial(String dialString) throws CallStateException {
return dial(dialString, null);
}
public Connection dial(String dialString, UUSInfo uusInfo) throws CallStateException {
.....
mCT.dial(.....);
}
// GsmCallTracker.java
GSMCallTracker(GSMPhone phone) {
cm = phone.mCM;
}
Connection dial(String dialString, int clirMode, UUSInfo uusInfo) {
cm.dial(........);
}
再往上看 mCM 的宣告:public CommandsInterface mCM;。這說明 GsmCallTracker 持有的是 CommandsInterface,也就是 RIL.java 的物件,因此 cm.dial(...) 就是在呼叫 RIL 的 dial 方法。
RIL 負責把上層的通話請求依既定協議格式打包,透過 socket 送給 rild 常駐程式,再由它轉發給基帶晶片。至此,一次撥號在 Java 層的旅程就結束了。
把這條鏈路記下來有個實際好處:當通話相關功能出現問題時,可以依「UI 層 → Phone 應用 → CallManager → GSMPhone → GsmCallTracker → RIL」的順序逐段定位,判斷是 Intent 沒送對、Action 轉換有誤,還是請求根本沒落到 RIL。
Android ContentProvider 使用指南:共享資料與 URI 設計
ContentProvider 用於對外共享資料,其他應用可透過 ContentResolver 進行增刪改查。註冊時需設定 authorities,外部則以 conte
Android 應用本地化實作指南
一款優秀的 Android 應用,往往要面對來自世界各地、使用不同語言的用戶。想要讓應用真正貼近每一個地區的使用者,就必須妥善處理文字、數字、日期乃至檔案格式等元素,讓它
Android ApiDemo 系列四:自訂 Dialog 主題
在 Android 中,Activity 的外觀可以透過 Theme 調整。ApiDemo 的 Custom Dialog 範例將一個 Activity 套用自訂主題,使
Android ViewPager 多頁滑動與頁卡動畫實作
本文以類似行動社群應用的頁卡體驗為例,示範如何運用 ViewPager 完成多頁面的手勢切換,並讓頂部頁卡下方的白色橫條在切換時平滑移動。先認識 ViewPagerVie