我们知道当发送EVENT_REGISTER_NETWORK_AGENT命令时的出来函数 handleRegisterNetworkAgent 来注册一个网络类型 而这个EVENT_REGISTER_NETWORK_AGENT 是有ConnectivityService的registerNetworkAgent 发送出的 3900 public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, 3901 LinkProperties linkProperties, NetworkCapabilities networkCapabilities, 3902 int currentScore, NetworkMisc networkMisc) { 3903 enforceConnectivityInternalPermission(); 3904 3905 // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network 3906 // satisfies mDefaultRequest. 3907 final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), 3908 new Network(reserveNetId()), new NetworkInfo(networkInfo), new LinkProperties( 3909 linkProperties), new NetworkCapabilities(networkCapabilities), currentScore, 3910 mContext, mTrackerHandler, new NetworkMisc(networkMisc), mDefaultRequest, this); 3911 synchronized (this) { 3912 nai.networkMonitor.systemReady = mSystemReady; 3913 } 3914 addValidationLogs(nai.networkMonitor.getValidationLogs(), nai.network); 3915 if (DBG) log("registerNetworkAgent " + nai); 3916 mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai)); 3917 return nai.network.netId; 3918 } 如上面的3916行发送注册网络类型的message. 那具体是怎么调用registerNetworkAgent的呢?我们以wifi为例 xref: /frameworks/opt/net/wifi/service/java/com/android/server/wifi/WifiStateMachine.java private class WifiNetworkAgent extends NetworkAgent { 7978 public WifiNetworkAgent(Looper l, Context c, String TAG, NetworkInfo ni, 7979 NetworkCapabilities nc, LinkProperties lp, int score) { 7980 super(l, c, TAG, ni, nc, lp, score); 7981 } 实现NetworkAgent的子类,调用父类的构造函数,父类构造上述如下 208 public NetworkAgent(Looper looper, Context context, String logTag, NetworkInfo ni, 209 NetworkCapabilities nc, LinkProperties lp, int score, NetworkMisc misc) { 210 super(looper); 211 LOG_TAG = logTag; 212 mContext = context; 213 if (ni == null || nc == null || lp == null) { 214 throw new IllegalArgumentException(); 215 } 216 217 if (VDBG) log("Registering NetworkAgent"); 218 ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService( 219 Context.CONNECTIVITY_SERVICE); 220 netId = cm.registerNetworkAgent(new Messenger(this), new NetworkInfo(ni), 221 new LinkProperties(lp), new NetworkCapabilities(nc), score, misc); 222 } 可以看到220 宏调用ConnectivityManager的registerNetworkAgent,通过binder调用到ConnectivityService的registerNetworkAgent。从而发送EVENT_REGISTER_NETWORK_AGENT event
如何注册NetworkInfo
本文解析了Android系统中通过ConnectivityManager的registerNetworkAgent方法注册网络代理的过程。从Wi-Fi网络代理的具体实现入手,详细介绍了如何通过Binder机制调用ConnectivityService中的registerNetworkAgent方法,并最终触发EVENT_REGISTER_NETWORK_AGENT事件。
摘要由CSDN通过智能技术生成