Android中打電話的資料流程

來源:互聯網
上載者:User

1. 所有流程的起點是從撥號後按下撥號鍵開始,此步的代碼在/android sourcecode/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));.............}

從newDialNumberIntent的定義可以看出,當撥號鍵按下以後,TwelveKeyDial會啟動一個特定的組件,該組件的ACTION為:ACTION_CALL_PRIVILEGED,經過尋找,該ACTION啟動的組件是目下:/android sourcecode/packeges/Phone/的一個檔案,在該檔案下的AndroidMenifest.xml中可以查到:“ACTION_CALL_PRIVILEGED”啟動的Activity的名字是:PrivilegedOutgoingCallBroadcast,但是我們到/android
sourcecode/packeges/Phone/src/....目下並找不到該檔案,因為該檔案在AndroidMenifest.xml中標記有點特殊:

<activity-alias />,這個標籤的意思是這個Activity是另一個Activity的別名,真實的Activity在標籤中用“android:targetActivity = OutgoingCallBroadcast”標出,所以 “ACTION_CALL_PRIVILEGED”啟動的 PrivilegedOutgoingCallBroadcast 所對應的真實“身份”是 “OutgoingCallBroadcast”。

2. 這個時候電話的資料已經流到OutgoingCallBroadcast.java中了。

在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_PRIVILEGED”,那麼根據所輸入的號碼是否是緊急號碼進行轉換,如果是緊急號碼,則ACTION = Intent.ACTION_CALL_EMERGENCY,否則ACTION = Intent.ACTION_CALL,並啟動轉換Activity :InCallScreen.java

3. InCallScreen.java依然在目錄/packeges/Phone/src/com/android/phone下。

InCallScreen的onCreate中調用initInCallScreen初始化打電話介面,並調用registerForPhoneStates註冊電話狀態監聽.

在onNewIntent()方法中有:

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);}}//placeCallprivate InCallInitStatus placeCall(Intent intent) {..............int callStatus  = PhoneUtils.placeCall(........);}

InCallScreen.java中的placeCall方法調用PhoneUtils.java檔案的placeCall方法。

4. PhoneUtils.java依然在目錄/packeges/Phone/src/com/android/phone下。

public static int placeCall(...) {Connection connection;connection = PhoneApp.getInstance().mCM.dial(phone, numberToDial);}

繼續追蹤,在PhoneApp.java中發現,mCM是CallManager.java類的一個對象,而CallManager.java是屬於frameworks層的,所以,這個時候資料流已經進入frameworks.

5. 進入/frameworks/base/telephony/java/com/android/internal/telephony目錄。

在CallManager.java的dial()方法中,有:

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.java抽象類別實現了介面Phone.java,而GSMPhone.java又實現了抽象類別PhoneBase,所以:

上述代碼中:phone.getForegroundCall()實際相當於GSMPhone對象執行了getForegroundCall()方法。

6. 繼續追蹤GSMPhone.java,該類位於/frameworks/base/telephony/java/com/android/internal/telephony/gsm/下。

GSMPhone.java:GsmCallTracker mCT;public GsmCall getForegroundCall() {return mCT.foregroundCall;}

可以看出getForegroundCall()函數繼續調用GsmCallTracker.java的foregroundCall屬性。

7.GsmCallTracker.java位於/frameworks/base/telephony/java/com/android/internal/telephony/gsm/下.

GsmCallTracker.java:GSMCall foregroundCall = new GSMCall(this);

開啟GSMCall.java,找到getPhone()方法,發現:

GSMCallTracker owner;public Phone getPhone() {return owner.phone;}

而在GSMCallTracker.java中有如下聲明:

GSMPhone phone;

到此,我們得出一下結論:第5部分標記紅色的代碼所返回的就是GSMPhone的對象,進一步可以得出,第5部分藍色標記的代碼即是調用了GSMPhone對象的dial方法。

8. 在GSMPhone.java中:

GSMCallTracker mCT;public Connection dial(String dialString) throws CallStateException {return dial(dialString, null);}public Connection dial(String dialString, UUSInfo uusInfo) throws CallStateException {.......mCT.dial(.......);}

繼續調用GSMCallTracker.java中的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()方法。

9. RIL.java

BOSS出現。

RIL對象負責把用戶端的通話請求按照一定的格式發送給"rild"socket,至此,請求過程完畢。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.