欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中打电话的数据流程分析

程序员文章站 2023-12-02 18:53:04
1.所有流程的起点是从拨号后按下拨号键开始,此步的代码在/android sourcecode/packages/contacts/src/com/android/cont...
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>