亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 2.3 撥號(hào)上網(wǎng)流程從源碼角度進(jìn)行分析

 更新時(shí)間:2013年01月16日 17:28:16   作者:  
SIM卡實(shí)現(xiàn)撥號(hào)上網(wǎng)功能之前需要設(shè)置一番,這些設(shè)置步驟究竟做了哪些事情呢?我們現(xiàn)在就從源碼的角度進(jìn)行分析
通常,如果我們想使用SIM卡撥號(hào)上網(wǎng)功能,我們要在設(shè)置中進(jìn)行簡(jiǎn)單的配置,步驟如下
設(shè)置 -》無(wú)線和網(wǎng)絡(luò) -》移動(dòng)網(wǎng)絡(luò) -》(已啟用數(shù)據(jù)/數(shù)據(jù)漫游/接入點(diǎn)名稱(chēng)/僅使用2G網(wǎng)絡(luò)/網(wǎng)絡(luò)運(yùn)營(yíng)商)
我們必須選中其中的“已啟用數(shù)據(jù)”選項(xiàng),然后配置接入點(diǎn)名稱(chēng)后就可以上網(wǎng)了,當(dāng)然有的設(shè)置中已經(jīng)根據(jù)你的SIM卡類(lèi)型默認(rèn)設(shè)置了接入點(diǎn),這時(shí)候你只選擇“已啟用數(shù)據(jù)”項(xiàng)后就可以完成上網(wǎng)功能設(shè)置。
這些設(shè)置步驟究竟做了哪些事情呢?我們現(xiàn)在就從源碼的角度進(jìn)行分析。

1. 首先,我們找到“移動(dòng)網(wǎng)絡(luò)”的設(shè)置UI-------Settings.java(/packages/apps/Phone/src/com/android/phone/Settings.java)
Settings.java:
"已啟用數(shù)據(jù)"選項(xiàng)的相關(guān)代碼如下:
復(fù)制代碼 代碼如下:

......
else if (preference == mButtonDataEnabled) {
if (DBG) log("onPreferenceTreeClick: preference == mButtonDataEnabled.");
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
cm.setMobileDataEnabled(mButtonDataEnabled.isChecked());
return true;
}
......

代碼中,我們得到一個(gè)ConnectivityManager對(duì)象,并調(diào)用該對(duì)象的setMobileDataEnable(boolean b)方法,根據(jù)傳入的參數(shù)進(jìn)行設(shè)置,我們看一下ConnectivityManager類(lèi)。

2. ConnectivityManager.java(/frameworks/base/core/java/android/net/ConnectivityManager.java)
這個(gè)時(shí)候,數(shù)據(jù)已經(jīng)進(jìn)入frameworks層。
setMobileDataEnable()方法代碼如下:
復(fù)制代碼 代碼如下:

IConnectivityManager mService;
......
 public ConnectivityManager(IConnectivityManager service) {
        if (service == null) {
            throw new IllegalArgumentException(
                "ConnectivityManager() cannot be constructed with null service");
        }
        mService = service;
    }
......
public void setMobileDataEnabled(boolean enabled) {
try {
mService.setMobileDataEnabled(enabled);
} catch (RemoteException e) {
}
}

這里我們要知道IConnectivityManager類(lèi),是根據(jù)IConnectivityManager.aidl接口自動(dòng)生成的一個(gè)java類(lèi),而我們自己有一個(gè)Service則繼承了該類(lèi)的內(nèi)部類(lèi):Stub,在我們自己為撥號(hào)上網(wǎng)實(shí)現(xiàn)的這個(gè)Service就是ConnectivityService,所以根據(jù)AIDL只是,我們知道,代碼中的mService其實(shí)就是ConnectivityService類(lèi)的對(duì)象,所以代碼在這里實(shí)際上是調(diào)用了ConnectivityService對(duì)象的setMobileDataEnable()方法。

3. ConnectivityService.java(/frameworks/./base/services/java/com/android/server/ConnectivityService.java)
setMobileDataEnable()方法代碼如下:
復(fù)制代碼 代碼如下:

public void setMobileDataEnabled(boolean enabled) {
enforceChangePermission();
if (DBG) Slog.d(TAG, "setMobileDataEnabled(" + enabled + ")");
mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_MOBILE_DATA,
(enabled ? ENABLED : DISABLED), 0));
}

這里發(fā)送了一個(gè)消息出去,mHandler收到該消息以后:
復(fù)制代碼 代碼如下:

case EVENT_SET_MOBILE_DATA:
{
boolean enabled = (msg.arg1 == ENABLED);
handleSetMobileData(enabled);
break;
}

收到該消息后,調(diào)用handleSetMobileData()方法:
復(fù)制代碼 代碼如下:

private NetworkStateTracker mNetTrackers[];
......
private void handleSetMobileData(boolean enabled) {
        ......
if (enabled) {
if (mNetTrackers[ConnectivityManager.TYPE_MOBILE] != null) {
if (DBG) {
Slog.d(TAG, "starting up " + mNetTrackers[ConnectivityManager.TYPE_MOBILE]);
}
mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();
}
            ......
}
 }

如果“已啟用數(shù)據(jù)”選項(xiàng)已經(jīng)選擇,那這個(gè)時(shí)候傳進(jìn)來(lái)的參數(shù)“enabled”應(yīng)該是“true”,所以會(huì)處理代碼中if語(yǔ)句塊,即執(zhí)行:
復(fù)制代碼 代碼如下:

mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect();

而在ConnectivityManager中,TYPE_MOBILE 為 0,所以這里相當(dāng)于調(diào)用了
復(fù)制代碼 代碼如下:

mNetTracker[0].reconnect()

但是,NetworkStateTracker是一個(gè)抽象類(lèi),所以具體的事情要交給它的子類(lèi)MobileDataStateTracker.java來(lái)干。

4. MobileDataStateTracker.java(/frameworks/base/core/java/android/net/MobileDataStateTracker.java)
該類(lèi)包含多種數(shù)據(jù)連接,包括MMS,SUPL,DUN等,
在MobileDataStateTracker.java里面的調(diào)用流程是這樣的:
復(fù)制代碼 代碼如下:

<PRE class=java name="code">mPhoneService = ITelephony.Stub.asInterface(ServiceManager.getService("phone"));</PRE>......<BR>
reconnect->mPhoneService.enableApnType(apnType);<P></P>
<PRE></PRE>
mPhoneService是電話的服務(wù)的客戶端,它的server端實(shí)際上是PhoneInterfaceManager對(duì)象
<P></P>
<P>5. PhoneInterfaceManager.java(/packages/apps/Phone/src/com/android/phone/PhoneInterfaceManager.java)<BR>
</P>
<P>看PhoneInterfaceManager的enableApnType方法:</P>
<P><PRE class=java name="code"> public int enableApnType(String type) {
enforceModifyPermission();
return mPhone.enableApnType(type);
}
</PRE><P></P>
這樣,就將連接apn的請(qǐng)求發(fā)送到telephony框架層下去了。apn在設(shè)置應(yīng)用里面有指定,一般在你的工程目錄下的system/etc/apns-conf.xml文件<BR>
<BR>
<P>6. 上面的mPhone是PhoneProxy對(duì)象,</P>
<P>調(diào)用流程:</P>
<P>PhoneProxy.java:<BR>
</P>
<P><PRE class=java name="code">mActivePhone.enableApnType(type)</PRE>mActivePhone是GSMPhone或者CDMAPhone的上溯接口PhoneBase對(duì)象<BR>
<P></P>
<P>PhoneBase.java:</P>
<P><PRE class=java name="code">mDataConnection.enableApnType(type);</PRE><P></P>
<P>調(diào)用到&nbsp;DataConnectionTracker的enableApnType方法</P>
<P>DataConnectionTracker.java:<BR>
</P>
<P>enableApnType(String type)->setEnabled->onEnableApn->onEnableNewApn<BR>
</P>
<BR>
<P>onEnableNewApn方法在DataConnectionTracker的派生類(lèi)GsmDataConnectionTracker和CdmaDataConnectionTracker中實(shí)現(xiàn),從而區(qū)別不同類(lèi)型PHONE的數(shù)據(jù)連接流程。<BR>
</P>
<P>以GSM為例,調(diào)用流程:onEnableNewApn->cleanUpConnection->conn.disconnect<BR>
<BR>
</P>
conn是DataConnection對(duì)象,標(biāo)識(shí)一鐘數(shù)據(jù)連接,可以看出這里實(shí)際上實(shí)現(xiàn)了一個(gè)數(shù)據(jù)連接的狀態(tài)機(jī)。<BR>
<P>在DataConnection對(duì)象里面數(shù)據(jù)連接的狀態(tài)分為:</P>
<P><PRE class=java name="code">DcDefaultState,默認(rèn)狀態(tài)。
DcInactiveState,非激活狀態(tài)。
DcActivatingState,正在激活狀態(tài)
DcActiveState,激活狀態(tài)
DcDisconnectingState,正在斷開(kāi)狀態(tài)
DcDisconnectingBadDnsState,斷開(kāi)狀態(tài)(因?yàn)殄e(cuò)誤的DNS)
</PRE><P></P>
<P>連接成功以后,notifyDefaultData調(diào)用到DefaultPhoneNotifier的notifyDataConnection方法。</P>
<P>DefaultPhoneNotifier是ITelephonyRegistry接口的客戶端,其服務(wù)端是TelephonyRegistry(com.android.server.TelephonyRegistry)</P>
<P>TelephonyRegistry的notifyDataConnection方法調(diào)用如下語(yǔ)句<BR>
<PRE class=java name="code"> r.callback.onDataConnectionStateChanged(state, networkType);</PRE><P></P>
<P>r是當(dāng)前mRecords中的元素,包含有IPhoneStateListener接口的實(shí)現(xiàn)callback,TelephonyRegistry中的每個(gè)調(diào)用都會(huì)遍歷mRecords中的元素,如果某個(gè)元素注冊(cè)了對(duì)應(yīng)接聽(tīng),</P>
<P>則調(diào)用callback的某個(gè)函數(shù)。</P>
<P>客戶端通過(guò)如下方式調(diào)用取得電話狀態(tài)的監(jiān)聽(tīng), 以StatusBarPolicy.java中的mPhoneStateListener為例:</P>
<P>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;((TelephonyManager)mContext.getSystemService(Context.TELEPHONY_SERVICE))</P>
<P>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.listen(mPhoneStateListener,<BR>
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PhoneStateListener.LISTEN_SERVICE_STATE<BR>
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| PhoneStateListener.LISTEN_SIGNAL_STRENGTHS<BR>
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| PhoneStateListener.LISTEN_CALL_STATE<BR>
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| PhoneStateListener.LISTEN_DATA_CONNECTION_STATE<BR>
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| PhoneStateListener.LISTEN_DATA_ACTIVITY);<BR>
</P>
<P>mPhoneStateListener是PhoneStateListener實(shí)例,PhoneStateListener實(shí)現(xiàn)了IPhoneStateListener接口,假如你繼承PhoneStateListener子類(lèi),首先你要確定你感興趣的監(jiān)聽(tīng)</P>
<P>事件,然后重寫(xiě)對(duì)應(yīng)的方法。再像上面那樣調(diào)用listen方法就可以了。</P>
<P>TelephonyRegistry的方法、監(jiān)聽(tīng)動(dòng)作、已經(jīng)你要重寫(xiě)的方法對(duì)應(yīng)關(guān)系如下:</P>
<P>TelephonyRegistry的方法 &nbsp;---------------------監(jiān)聽(tīng)動(dòng)作-------------------------------------------------------PhoneStateListener子類(lèi)中的中的回調(diào)<BR>
</P>
<P>notifyServiceState&nbsp;&nbsp; ---------- PhoneStateListener.LISTEN_SERVICE_STATE &nbsp; &nbsp; &nbsp; ----------------- &nbsp;public void onServiceStateChanged(ServiceState state)&nbsp;<BR>
</P>
<P>notifySignalStrength&nbsp;&nbsp; -------&nbsp;PhoneStateListener.LISTEN_SIGNAL_STRENGTHS &nbsp; &nbsp; --------- -- &nbsp;public void onSignalStrengthsChanged(SignalStrength signalStrength)<BR>
</P>
<P>notifyCallState &nbsp;----------------&nbsp;PhoneStateListener.LISTEN_CALL_STATE &nbsp; &nbsp;------------------------- &nbsp;&nbsp;public void onCallStateChanged(int state, String incomingNumber)<BR>
</P>
<P>notifyDataConnection -------&nbsp;PhoneStateListener.LISTEN_DATA_CONNECTION_STATE &nbsp; &nbsp;--- &nbsp;&nbsp;public void onDataConnectionStateChanged(int state, int networkType)<BR>
</P>
<P>notifyDataActivity &nbsp;--------------&nbsp;PhoneStateListener.LISTEN_DATA_ACTIVITY ----------------------- &nbsp;&nbsp;public void onDataActivity(int direction)<BR>
</P>
<P>。。。。。。。。</P>
<P>因此整個(gè)調(diào)用鏈?zhǔn)牵篋efaultPhoneNotifier:notifyDataConnection ---------》&nbsp;TelephonyRegistry :notifyDataConnection---------》</P>
<P>PhoneStateListener.callback:onDataConnectionStateChanged --------------》PhoneStateListener子類(lèi)的onDataConnectionStateChanged</P>
<P>除此之外,TelephonyRegistry還發(fā)出一個(gè)ACTION_ANY_DATA_CONNECTION_STATE_CHANGED,包含數(shù)據(jù)連接的詳細(xì)信息。</P>
<P><BR>
而Mobile Data Service里面的MobileDataStateTracker會(huì)接收到這個(gè)動(dòng)作,由它的BoadcastReceiver類(lèi)MobileDataStateReceiver提取出數(shù)據(jù)連接的信息,然后設(shè)置好狀態(tài)</P>
<PRE class=java name="code">setDetailedState(DetailedState.CONNECTING, reason, apnName);
</PRE>
<P>MobileDataStateTracker根據(jù)狀態(tài)變化給ConnectivityService發(fā)送EVENT_STATE_CHANGED消息。</P>
<P>ConnectivityService調(diào)用handleConnect去執(zhí)行相關(guān)炒作,包括關(guān)閉優(yōu)先級(jí)比它低的數(shù)據(jù)連接,更新?tīng)顟B(tài)欄等等。<BR>
</P>
<P>還有很多地方還沒(méi)有搞明白,以后再續(xù)。<BR>
</P>
<P><BR>
</P>
<P><BR>
</P>
<BR>
<P><BR>
<BR>
</P>

相關(guān)文章

最新評(píng)論