編輯:關於 Android 程式設計
絕大多數情況下,使用者是透過 dialer 輸入號碼並按下撥號鍵完成通話,所以我們就從 dialer 這個入口開始往下追。
DialpadFragment 就是撥號界面的 Fragment。當使用者點擊撥號按鈕時,會回呼它的 onClick:
public void onClick(View view) {
switch (view.getId()) {
case R.id.dialpad_floating_action_button:
view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
handleDialButtonPressed();
break;
}
}
handleDialButtonPressed 的核心邏輯是組出一個 Intent 並啟動它:
final Intent intent = CallUtil.getCallIntent(number);
if (!isDigitsShown) {
// 必須是會議通話時,補上這個 extra
intent.putExtra(EXTRA_DIAL_CONFERENCE_URI, true);
}
intent.putExtra(ADD_PARTICIPANT_KEY, mAddParticipant && isPhoneInUse());
DialerUtils.startActivityWithErrorToast(getActivity(), intent);
hideAndClearDialpad(false);
這裡組出的 Intent 帶的 action 是 Intent.ACTION_CALL,它對應到 /packages/services/Telephony 模組中的 OutgoingCallBroadcaster,而這是一個 Activity。
public static Intent getCallIntent(String number) {
return getCallIntent(getCallUri(number));
}
public static Intent getCallIntent(Uri uri) {
return new Intent(Intent.ACTION_CALL, uri);
}
進入這個 Activity 後,onCreate 會把 Intent 交給 processIntent 處理。processIntent 主要負責三種 action:CALL(一般對外語音通話)、CALL_PRIVILEGED(內建應用如聯絡人、語音撥號、藍牙所發出)與 CALL_EMERGENCY(來自鎖定畫面可達的緊急撥號器)。
對於 tel: URI 的電話,流程是 OutgoingCallReceiver → SipCallOptionHandler → InCallScreen;對於 sip: URI 的網路電話,則跳過 NEW_OUTGOING_CALL 廣播,直接走 SipCallOptionHandler → InCallScreen;voicemail: URI 的語音信箱處理方式與電話類似。
processIntent 做了幾件關鍵的事:先確認裝置具備語音能力,否則直接返回;接著從 Intent 取出號碼並清除分隔符號;然後透過 AppOpsManager 檢查發出 ACTION_CALL 的 uid 與 package 是否具有 OP_CALL_PHONE 權限,沒有就 finish 掉。若為緊急號碼或號碼合法,callNow 為 true,會直接跳到 InCallScreen,不發送 NEW_OUTGOING_CALL 廣播;否則組出一個 ACTION_NEW_OUTGOING_CALL 的有序廣播,交由 OutgoingCallReceiver 攔截處理,並掛上逾時訊息作為保險。
public void placeCall(Intent intent) {
// 只接受 CALL / CALL_EMERGENCY / CALL_PRIVILEGED 三種 action
Phone phone = mApp.mCM.getDefaultPhone();
if (TelephonyCapabilities.supportsOtasp(phone)) {
checkForOtaspCall(intent);
}
CallStatusCode status = placeCallInternal(intent);
switch (status) {
case SUCCESS:
case EXITED_ECM:
break;
default:
handleOutgoingCallError(status);
break;
}
}
無論成功與否,最後都會顯示 InCallScreen,並依錯誤碼給出對應提示。placeCallInternal 則把實際工作交給 PhoneUtils.placeCall,由它與 RIL 建立連線、設定音頻模式與免持狀態。
CallManager 會先檢查當前狀態是否允許撥號,若不允许則嘗試當作 in-call MMI 指令處理,否則拋出 CallStateException。而 basePhone 多半是 Phone 的代理類 PhoneProxy,會依 mActivePhone 判斷真正的實作,可能是 GSMPhone、CDMAPhone 或 SipPhone。
protected Connection dialInternal(String dialString, UUSInfo uusInfo, int videoState, Bundle intentExtras) {
String newDialString = PhoneNumberUtils.stripSeparators(dialString);
if (handleInCallMmiCommands(newDialString)) {
return null;
}
String networkPortion = PhoneNumberUtils.extractNetworkPortionAlt(newDialString);
GsmMmiCode mmi = GsmMmiCode.newFromDialString(networkPortion, this, mUiccApplication.get());
if (mmi == null) {
return mCT.dial(newDialString, uusInfo, intentExtras);
} else if (mmi.isTemporaryModeCLIR()) {
return mCT.dial(mmi.mDialingNumber, mmi.getCLIRMode(), uusInfo, intentExtras);
}
mPendingMMIs.add(mmi);
mMmiRegistrants.notifyRegistrants(new AsyncResult(null, mmi, null));
mmi.processCode();
return null;
}
也就是說,號碼會先被判定是 MMI 指令(如 *#06#)還是真正的電話號碼,兩條路之後都由 GsmCallTracker#dial 接手。
public void dial(String address, int clirMode, UUSInfo uusInfo, Message result) {
RILRequest rr = RILRequest.obtain(RIL_REQUEST_DIAL, result);
rr.mParcel.writeString(address);
rr.mParcel.writeInt(clirMode);
// ... 寫入 UUS 資訊
send(rr);
}
最終仍然是透過 send 方法把 EVENT_SEND 訊息丟給自己處理——有意思的是,這裡居然和簡訊的發送走到同一條路上,之後就交給 modem 去執行真正的動作。
Android 入門實作:打造可點擊的 Button
不論是為了日後的工作需求,還是眼下的專案,掌握行動開發都是一項值得投資的技能。而學習任何新技術,總得從最經典的 Hello World 開始,Android 自然也不例外
Android 中使用 SAX 機制解析 XML 文件详解
前言 本文主要介紹在 Android 中如何解析 XML 文件,核心採用的是 SAX 機制。SAX 全稱為 Simple API for XML,它既是一套接口,也是一個
Android 中用程式碼動態佈局:依資料量自動生成介面
有時候我們需要根據不同的需求動態決定 App 頁面的佈局,例如依伺服器回傳的條目數量,來決定頁面上要顯示幾個控件、哪些圖示等。本文就來示範如何用 Java 程式碼完成動態
Android 開發環境變量配置圖文教學
無論使用 ADT 還是 Android Studio 開發,都需要先下載 JDK(官網 https://www.oracle.com/index.html)。安裝完成後,