android - 如何在 Android 9 上赋予 WiFi 高于以太网的优先级?

标签 android android-source android-networking android-9.0-pie android-connectivitymanager

我正在使用 android 9,我想优先使用 WiFi over Ethernet。 我尝试在我的 config.xml 文件中为 wifi 赋予比以太网更高的优先级,如下所示,但我的以太网仍然具有更高的优先级。

<string-array translatable="false" name="networkAttributes">
        <item>"wifi,1,1,2,6000,true"</item>
        <item>"ethernet,9,9,0,6000,true"</item>
</string-array>

我在网上搜索了一下,发现可以在ConnectivityManager.java中给出默认的网络偏好。但它在 API28 中显示已弃用。

@Deprecated
    public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;

此外,getNetworkInfostartUsingNetworkFeature 也已弃用。

@Deprecated
    @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
    public NetworkInfo getNetworkInfo(int networkType) {
@Deprecated
    public int startUsingNetworkFeature(int networkType, String feature) {

如何在 Android 9 上将 WIFI 设置为高于以太网的优先级?

最佳答案

关于如何处理这个问题,您有两种选择。

选项 1:更新 AOSP 源代码

在 Android 9 中,ConnectivityService 将使用静态网络得分 来确定使用各种网络传输类型(以太网、Wi-Fi 还是蜂窝等)的优先级。

以太网的网络得分为 70 ( link ),而 Wi-Fi 的网络得分为 60 ( link )。

在您的情况下,如果我想优先考虑 Wi-Fi 而不是以太网,您可以更改分数以反射(reflect)新的优先级(例如,在上面的 Wi-Fi 链接中将 Wi-Fi 更改为 71,或者类似地,将以太网的分数降低到在它的工厂里说 59)。

Wi-Fi 示例:

private static final int SCORE_FILTER = 71;

选项 2:使用资源覆盖

有一个资源覆盖层可用于在名为 config_ethernet_interfaces ( link ) 的以太网网络上手动配置网络功能。

<!-- Configuration of Ethernet interfaces in the following format:
         <interface name|mac address>;[Network Capabilities];[IP config];[Override Transport]
         Where
               [Network Capabilities] Optional. A comma seprated list of network capabilities.
                   Values must be from NetworkCapabilities#NET_CAPABILITY_* constants.
                   The NOT_ROAMING, NOT_CONGESTED and NOT_SUSPENDED capabilities are always
                   added automatically because this configuration provides no way to update
                   them dynamically.
               [IP config] Optional. If empty or not specified - DHCP will be used, otherwise
                   use the following format to specify static IP configuration:
                       ip=<ip-address/mask> gateway=<ip-address> dns=<comma-sep-ip-addresses>
                       domains=<comma-sep-domains>
               [Override Transport] Optional. An override network transport type to allow
                    the propagation of an interface type on the other end of a local Ethernet
                    interface. Value must be from NetworkCapabilities#TRANSPORT_* constants. If
                    left out, this will default to TRANSPORT_ETHERNET.
         -->
    <string-array translatable="false" name="config_ethernet_interfaces">
        <!--
        <item>eth1;12,13,14,15;ip=192.168.0.10/24 gateway=192.168.0.1 dns=4.4.4.4,8.8.8.8</item>
        <item>eth2;;ip=192.168.0.11/24</item>
        <item>eth3;12,13,14,15;ip=192.168.0.12/24;1</item>
        -->
    </string-array>

这是在以太网接口(interface)名称上建立索引,因此您需要在所有情况下都使用相同的以太网名称,例如eth0 适合大多数人。您可以更新上述配置以使用这些功能。在您的情况下,您可以省略 NOT_RESTRICTED 功能 ( link ),在这种情况下,以太网永远不会用作默认网络,只会让 Wi-Fi 优先更高。

  <!-- Restricting eth0 -->
  <string-array translatable="false" name="config_ethernet_interfaces">
    <item>eth0;11,12,14;;</item>
  </string-array>

事实上,出于类似原因,Android Cuttlefish 目标 ( link) 今天也是这样做的。请注意,上面的配置会将 eth0 标记为受限,因此它可能不会获得 IP 或根本无法使用(网络访问受限的潜在应用程序除外)。如果您想要不同的行为,您可以随时使用这些功能。

关于android - 如何在 Android 9 上赋予 WiFi 高于以太网的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70260615/

相关文章:

android - SQLite数据库错误插入

android - 如何使用我自己的 .img 分区启动 android 模拟器?

android - 改造得到字符串响应

Android 互联网连接检查失败

android - eclipse 不在 logcat 中仅显示 galaxy nexus 的时间?

android - 如何在 Android 6 到 Android 12 的所有版本上使用 adb shell 命令发送短信?

android - 如何仅同步 android 2.2 froyo 代码?

android - AOSP中哪里是解析AndroidManifest.xml的代码?

android - OkHttp 路由流量抛出 wifi 和 SocketFactory

java - Android 中的线程