gpt4 book ai didi

Android 2.3 拨号上网流程从源码角度进行分析

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 26 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Android 2.3 拨号上网流程从源码角度进行分析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

通常,如果我们想使用SIM卡拨号上网功能,我们要在设置中进行简单的配置,步骤如下: 设置 -》无线和网络 -》移动网络 -》(已启用数据/数据漫游/接入点名称/仅使用2G网络/网络运营商) 我们必须选中其中的“已启用数据”选项,然后配置接入点名称后就可以上网了,当然有的设置中已经根据你的SIM卡类型默认设置了接入点,这时候你只选择“已启用数据”项后就可以完成上网功能设置。 这些设置步骤究竟做了哪些事情呢?我们现在就从源码的角度进行分析。 1. 首先,我们找到“移动网络”的设置UI-------Settings.java(/packages/apps/Phone/src/com/android/phone/Settings.java) Settings.java: "已启用数据"选项的相关代码如下:

复制代码 代码如下

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

代码中,我们得到一个ConnectivityManager对象,并调用该对象的setMobileDataEnable(boolean b)方法,根据传入的参数进行设置,我们看一下ConnectivityManager类。 2. ConnectivityManager.java(/frameworks/base/core/java/android/net/ConnectivityManager.java) 这个时候,数据已经进入frameworks层。 setMobileDataEnable()方法代码如下:

复制代码 代码如下

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类,是根据IConnectivityManager.aidl接口自动生成的一个java类,而我们自己有一个Service则继承了该类的内部类:Stub,在我们自己为拨号上网实现的这个Service就是ConnectivityService,所以根据AIDL只是,我们知道,代码中的mService其实就是ConnectivityService类的对象,所以代码在这里实际上是调用了ConnectivityService对象的setMobileDataEnable()方法。 3. ConnectivityService.java(/frameworks/./base/services/java/com/android/server/ConnectivityService.java) setMobileDataEnable()方法代码如下:

复制代码 代码如下

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)); } 。

这里发送了一个消息出去,mHandler收到该消息以后:

复制代码 代码如下

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

收到该消息后,调用handleSetMobileData()方法:

复制代码 代码如下

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(); }             ...... }  } 。

如果“已启用数据”选项已经选择,那这个时候传进来的参数“enabled”应该是“true”,所以会处理代码中if语句块,即执行:

复制代码 代码如下

mNetTrackers[ConnectivityManager.TYPE_MOBILE].reconnect(),

而在ConnectivityManager中,TYPE_MOBILE 为 0,所以这里相当于调用了 。

复制代码 代码如下

mNetTracker[0].reconnect() 。

但是,NetworkStateTracker是一个抽象类,所以具体的事情要交给它的子类MobileDataStateTracker.java来干。 4. MobileDataStateTracker.java(/frameworks/base/core/java/android/net/MobileDataStateTracker.java) 该类包含多种数据连接,包括MMS,SUPL,DUN等, 在MobileDataStateTracker.java里面的调用流程是这样的:

复制代码 代码如下

<PRE class=java name="code">mPhoneService = ITelephony.Stub.asInterface(ServiceManager.getService("phone"));</PRE>......<BR> reconnect->mPhoneService.enableApnType(apnType);<P></P> <PRE></PRE> mPhoneService是电话的服务的客户端,它的server端实际上是PhoneInterfaceManager对象 <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的请求发送到telephony框架层下去了。apn在设置应用里面有指定,一般在你的工程目录下的system/etc/apns-conf.xml文件<BR> <BR> <P>6. 上面的mPhone是PhoneProxy对象,</P> <P>调用流程:</P> <P>PhoneProxy.java:<BR> </P> <P><PRE class=java name="code">mActivePhone.enableApnType(type)</PRE>mActivePhone是GSMPhone或者CDMAPhone的上溯接口PhoneBase对象<BR> <P></P> <P>PhoneBase.java:</P> <P><PRE class=java name="code">mDataConnection.enableApnType(type);</PRE><P></P> <P>调用到&nbsp;DataConnectionTracker的enableApnType方法</P> <P>DataConnectionTracker.java:<BR> </P> <P>enableApnType(String type)->setEnabled->onEnableApn->onEnableNewApn<BR> </P> <BR> <P>onEnableNewApn方法在DataConnectionTracker的派生类GsmDataConnectionTracker和CdmaDataConnectionTracker中实现,从而区别不同类型PHONE的数据连接流程。<BR> </P> <P>以GSM为例,调用流程:onEnableNewApn->cleanUpConnection->conn.disconnect<BR> <BR> </P> conn是DataConnection对象,标识一钟数据连接,可以看出这里实际上实现了一个数据连接的状态机。<BR> <P>在DataConnection对象里面数据连接的状态分为:</P> <P><PRE class=java name="code">DcDefaultState,默认状态。 DcInactiveState,非激活状态。 DcActivatingState,正在激活状态 DcActiveState,激活状态 DcDisconnectingState,正在断开状态 DcDisconnectingBadDnsState,断开状态(因为错误的DNS) </PRE><P></P> <P>连接成功以后,notifyDefaultData调用到DefaultPhoneNotifier的notifyDataConnection方法。</P> <P>DefaultPhoneNotifier是ITelephonyRegistry接口的客户端,其服务端是TelephonyRegistry(com.android.server.TelephonyRegistry)</P> <P>TelephonyRegistry的notifyDataConnection方法调用如下语句<BR> <PRE class=java name="code"> r.callback.onDataConnectionStateChanged(state, networkType);</PRE><P></P> <P>r是当前mRecords中的元素,包含有IPhoneStateListener接口的实现callback,TelephonyRegistry中的每个调用都会遍历mRecords中的元素,如果某个元素注册了对应接听,</P> <P>则调用callback的某个函数。</P> <P>客户端通过如下方式调用取得电话状态的监听, 以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实例,PhoneStateListener实现了IPhoneStateListener接口,假如你继承PhoneStateListener子类,首先你要确定你感兴趣的监听</P> <P>事件,然后重写对应的方法。再像上面那样调用listen方法就可以了。</P> <P>TelephonyRegistry的方法、监听动作、已经你要重写的方法对应关系如下:</P> <P>TelephonyRegistry的方法 &nbsp;---------------------监听动作-------------------------------------------------------PhoneStateListener子类中的中的回调<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>因此整个调用链是:DefaultPhoneNotifier:notifyDataConnection ---------》&nbsp;TelephonyRegistry :notifyDataConnection---------》</P> <P>PhoneStateListener.callback:onDataConnectionStateChanged --------------》PhoneStateListener子类的onDataConnectionStateChanged</P> <P>除此之外,TelephonyRegistry还发出一个ACTION_ANY_DATA_CONNECTION_STATE_CHANGED,包含数据连接的详细信息。</P> <P><BR> 而Mobile Data Service里面的MobileDataStateTracker会接收到这个动作,由它的BoadcastReceiver类MobileDataStateReceiver提取出数据连接的信息,然后设置好状态</P> <PRE class=java name="code">setDetailedState(DetailedState.CONNECTING, reason, apnName); </PRE> <P>MobileDataStateTracker根据状态变化给ConnectivityService发送EVENT_STATE_CHANGED消息。</P> <P>ConnectivityService调用handleConnect去执行相关炒作,包括关闭优先级比它低的数据连接,更新状态栏等等。<BR> </P> <P>还有很多地方还没有搞明白,以后再续。<BR> </P> <P><BR> </P> <P><BR> </P> <BR> <P><BR> <BR> </P> 。

最后此篇关于Android 2.3 拨号上网流程从源码角度进行分析的文章就讲到这里了,如果你想了解更多关于Android 2.3 拨号上网流程从源码角度进行分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com