Android中打電話的資料流程分析

NO IMAGE

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()方法中有:
複製程式碼 程式碼如下:
<PRE class=java name=”code”>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);
}</PRE><P></P>
<PRE></PRE>
在這個方法中,判斷如果所收到的ACTION是“ACTION_CALL_PRIVILEGED”,那麼根據所輸入的號碼是否是緊急號碼進行轉換,如果是緊急號碼,則ACTION = Intent.ACTION_CALL_EMERGENCY,否則ACTION = Intent.ACTION_CALL,並啟動轉換Activity :InCallScreen.java
<P></P>
<P>3. InCallScreen.java依然在目錄/packeges/Phone/src/com/android/phone下。</P>
<P>InCallScreen的onCreate中呼叫initInCallScreen初始化打電話介面,並呼叫registerForPhoneStates註冊電話狀態監聽.<BR>
</P>
<P>在onNewIntent()方法中有:</P>
<P><PRE class=java name=”code”>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);
}
}
//placeCall
private InCallInitStatus placeCall(Intent intent) {
…………..
int callStatus = PhoneUtils.placeCall(……..);
}</PRE>InCallScreen.java中的placeCall方法呼叫PhoneUtils.java檔案的placeCall方法。<BR>
<P></P>
<P>4. PhoneUtils.java依然在目錄/packeges/Phone/src/com/android/phone下。</P>
<P><PRE class=java name=”code”>public static int placeCall(…) {
Connection connection;
connection = PhoneApp.getInstance().mCM.dial(phone, numberToDial);
}</PRE>繼續追蹤,在PhoneApp.java中發現,mCM是CallManager.java類的一個物件,而CallManager.java是屬於frameworks層的,所以,這個時候資料流已經進入frameworks.<P></P>
<P>5. 進入/frameworks/base/telephony/java/com/android/internal/telephony目錄。</P>
<P>在CallManager.java的dial()方法中,有:</P>
<P><PRE class=java name=”code”><SPAN style=”BACKGROUND-COLOR: rgb(255,255,255); FONT-FAMILY: Arial, Verdana, sans-serif; WHITE-SPACE: normal”></SPAN><PRE class=java name=”code”>public Connection dial(Phone phone, String dialNumber) throws CallStateException {
Phone basePhone = getPhoneBase(phone);
Connection result;
<SPAN style=”COLOR: #3333ff”>result = basePhone.dial(dialString);</SPAN>
……..
}
private static Phone getPhoneBase(Phone phone) {
if (phone instanceof PhoneProxy) {
<SPAN style=”COLOR: #ff0000″>return phone.getForegroundCall().getPhone();</SPAN>
}
return phone;
}</PRE><P></P>
<PRE></PRE>
<PRE class=java name=”code”><SPAN style=”BACKGROUND-COLOR: rgb(255,255,255); FONT-FAMILY: Arial, Verdana, sans-serif; WHITE-SPACE: normal”>繼續追蹤會發現:</SPAN></PRE><PRE class=java name=”code”><SPAN style=”BACKGROUND-COLOR: rgb(255,255,255); FONT-FAMILY: Arial, Verdana, sans-serif; WHITE-SPACE: normal”>PhoneBase.java抽象類實現了介面Phone.java,而GSMPhone.java又實現了抽象類PhoneBase,所以:</SPAN></PRE>
<P></P>
<P>上述程式碼中:phone.getForegroundCall()實際相當於GSMPhone物件執行了getForegroundCall()方法。</P>
<P>6. 繼續追蹤GSMPhone.java,該類位於/frameworks/base/telephony/java/com/android/internal/telephony/gsm/下。</P>
<P><PRE class=java name=”code”>GSMPhone.java:
GsmCallTracker mCT;
public GsmCall getForegroundCall() {
return mCT.foregroundCall;
}</PRE>可以看出getForegroundCall()函式繼續呼叫GsmCallTracker.java的foregroundCall屬性。<P></P>
<P>7.GsmCallTracker.java位於/frameworks/base/telephony/java/com/android/internal/telephony/gsm/下.</P>
<P><PRE class=cpp name=”code”>GsmCallTracker.java:
GSMCall foregroundCall = new GSMCall(this);</PRE><P></P>
<P>開啟GSMCall.java,找到getPhone()方法,發現:</P>
<P><PRE class=java name=”code”>GSMCallTracker owner;
public Phone getPhone() {
return owner.phone;
}</PRE><P></P>
<P>而在GSMCallTracker.java中有如下宣告:</P>
<P><PRE class=cpp name=”code”>GSMPhone phone;</PRE><P></P>
<P><SPAN style=”COLOR: #ff0000″>到此,我們得出一下結論:第5部分標記紅色的程式碼所返回的就是GSMPhone的物件,</SPAN><SPAN style=”COLOR: #3333ff”>進一步可以得出,第5部分藍色標記的程式碼即是呼叫了GSMPhone物件的dial方法。</SPAN></P>
<P>8. 在GSMPhone.java中:</P>
<P><PRE class=java name=”code”>GSMCallTracker mCT;
public Connection dial(String dialString) throws CallStateException {
return dial(dialString, null);
}
public Connection dial(String dialString, UUSInfo uusInfo) throws CallStateException {
…….
mCT.dial(…….);
}</PRE><P></P>
<P>繼續呼叫GSMCallTracker.java中的dial()方法:</P>
<P><PRE class=cpp name=”code”>GSMCallTracker.java:
GSMCallTracker(GSMPhone phone) {
cm = phone.mCM;
}
Connection dial(String dialString, int clirMode, UUSInfo uusInfo) {
<SPAN style=”COLOR: #ff0000″>cm.dial(……..);</SPAN>
}</PRE>追蹤mCM,發現 :<P></P>
<P>public CommandsInterface mCM;</P>
<P>所以GSMCallTracker持有CommandsInterface物件,即RIL.Java類的物件,所以”cm.dial(….)”即是呼叫RIL類物件的dial()方法。</P>
<P>9. RIL.java</P>
<P>BOSS出現。</P>
<P>RIL物件負責把客戶端的通話請求按照一定的格式傳送給”rild”socket,至此,請求過程完畢。</P>
</PRE>

您可能感興趣的文章:

Android APP啟動方式、啟動流程及啟動優化分析分析Android中應用的啟動流程Android教程之開機流程全面解析從原始碼分析Android的Glide庫的圖片載入流程及特點Android系統關機的全流程解析Android Bluetooth藍芽技術使用流程詳解Android Mms之:簡訊傳送流程(圖文詳解)Android Mms之:接收資訊流程(圖文詳解)Android 2.3 撥號上網流程從原始碼角度進行分析Android程式設計輸入事件流程詳解