实战-Android 系统设置无线/有线优先连接

需求
系统起来之后,如果有线(ETH)插入和WIFI 打开的情况下,优先使用哪种连接方式。

实现
NetworkAgentInfo 中getCurrentScore 函数直接返回最大的Score 即可。

有线优先

frameworks/base/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
private int getCurrentScore(boolean pretendValidated) {
+	//Begin add for ETH higher priority
+    if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
+         return ConnectivityConstants.EXPLICITLY_SELECTED_NETWORK_SCORE;
+     }
+    //End add for ETH higher priority
}

无线优先

frameworks/base/services/core/java/com/android/server/connectivity/NetworkAgentInfo.java
private int getCurrentScore(boolean pretendValidated) {
+	//Begin add for ETH higher priority
+	if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
+            return MAXIMUM_NETWORK_SCORE;
+   }
+    //End add for ETH higher priority
}

参考:
Framework中的连接管理机制

你可能感兴趣的:(android,java)