Android11 wired network and wifi priority setting
Article directory
1. Introduction to the basic knowledge of priority
After Android 6.0, the priority setting in the system is based on the Score score, the score is 0-100, and the higher the value, the higher the priority.
System default score:
SIM卡网络 50
wifi网络 60
有线网络 70
Mobile phone network settings have their own Factory setting class, all inherited from NetworkFactory.java
wifi network setting class: WifiNetworkFactory.java
wired network setting class: EthernetNetworkFactory.java
The subclasses of NetworkFactory all have NETWORK_SCORE constants, which represent the score of the network.
2. Wired network priority setting
1. Set the wired network priority in Android9.0 and directly modify the NETWORK_SCORE value of EthernetNetworkFactory.java
private final static int NETWORK_SCORE = 55; //change score from 70
However, after I modified the code of Android11, I found that it did not take effect, and the wired network is still preferred.
After studying EthernetNetworkFactory.java and ConnectivityService.java, I found that the logic inside has been greatly modified.
2. Modify the wired network priority in Android11
Find the getNetworkScore() method of EthernetNetworkFactory.java, the score returned here is the effective score of the wired network;
This getNetworkScore() method is new in Android11.
Just return the NETWORK_SCORE value in this method. Many judgments in it are useless.
If you want wifi to have a higher priority than wired, you must set the score of wired network to be smaller than wifi. In the later test process, it is found that in some cases, the score of wifi will become 20. Set the score of wired network to 15. take effect.
Adb can be used to check the score, which is described at the end of the article.
3. Simple analysis of network priority
reference:
Connection management mechanism in Framework: https://blog.csdn.net/u010961631/article/details/48629601
NetworkFactory of network connection scoring mechanism: https://blog.csdn.net/u010961631/article/details/48971431
NetworkAgent of network connection scoring mechanism: https://blog.csdn.net/u010961631/article/details/48971651
Android network priority and change: https://blog.csdn.net/u013686019/article/details/51447129/
The codes on the Internet are relatively old and can only be used as a reference for ideas. Some methods in it are different.
1. The main logic of network switching is in ConnectivityService.java
ConnectivityService.java, like WMS and AMS, is started in System_server;
ConnectivityService.java will save and manage the network connection subclass of NetworkFactory.
Android11 ConnectivityService
added NetworkProviderInfo as an internal class, but the previous NetworkAgentInfo is still retained;
2. The priority scoring logic is mainly in the evalRequest method
The evalRequest method of NetworkFactory.java is the main judgment logic for scoring
private void evalRequest(NetworkRequestInfo n) {
if (VDBG) {
log("evalRequest");
log(" n.requests = " + n.requested);
log(" n.score = " + n.score);
log(" mScore = " + mScore);
log(" request.providerId = " + n.providerId);
log(" mProvider.id = " + mProvider.getProviderId());
}
if (shouldNeedNetworkFor(n)) { //通过一些属性值判断是否需要请求网络
if (DBG) log(" needNetworkFor");
needNetworkFor(n.request, n.score);
n.requested = true;
} else if (shouldReleaseNetworkFor(n)) { //通过一些属性值判断是否需要释放网络
if (DBG) log(" releaseNetworkFor");
releaseNetworkFor(n.request);
n.requested = false;
} else {
if (DBG) log(" done");
}
}
The specific situation and the operation of releasing the network are implemented in the subclass.
The network to be used must go through the needNetworkFor method.
You can add some more logs to determine whether to execute a certain method.
3. To understand the specific process of this network connection, you need to know a few things
(1)NetworkFactory.java
Related subclasses: WifiNetworkFactory, EthernetNetworkFactory
Inner class: NetworkRequestInfo
(2)ConnectivityService.java
Inner class: NetworkProviderInfo
####(3)Handler
The understanding of the Messenger object, not the Message object
Because NetworkFactory is inherited from Handler,
And many messages are sent and received through the Messenger object;
(4) Others
NetworkRequest network request object
NetworkAgent network proxy object, created in EthernetNetworkFactory
NetworkAgentInfo Network agent encapsulation object, created in ConnectivityService
Messages are transmitted through Messenger, ConnectivityService–”NetworkAgentInfo–”NetworkAgent–”specific Factory
Network network object, contained in the network proxy object
EthernetNetworkFactory.NetworkInterfaceState.start()–>new NetworkAgent
Vpn.agentConnect()–>new NetworkAgent
NetworkAgent.register()–>
ConnectivityManager.registerNetworkAgent–>
ConnectivityService.registerNetworkAgent–>new NetworkAgentInfo
If you want to view the Score corresponding to the current network,
you can use the adb shell dumpsys connectivity. The Current Networks inside has a lot of relevant data information, provided that you have to connect to the corresponding network first.
Detailed explanation of adb viewing and analyzing network conditions:
https://blog.csdn.net/wenzhi20102321/article/details/122161589