首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Android9上给予WiFi比以太网更高的优先级?

如何在Android9上给予WiFi比以太网更高的优先级?
EN

Stack Overflow用户
提问于 2021-12-07 13:00:18
回答 1查看 718关注 0票数 3

我正在研究Android9,我想优先考虑WiFi而不是以太网。我尝试在我的config.xml文件中赋予wifi比以太网更高的优先级,如下所示,但是我的以太网仍然具有更高的优先级。

代码语言:javascript
运行
复制
<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中不受欢迎。

代码语言:javascript
运行
复制
@Deprecated
    public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;

此外,getNetworkInfostartUsingNetworkFeature也不受欢迎。

代码语言:javascript
运行
复制
@Deprecated
    @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
    public NetworkInfo getNetworkInfo(int networkType) {
代码语言:javascript
运行
复制
@Deprecated
    public int startUsingNetworkFeature(int networkType, String feature) {

我如何给予WIFI比android 9上的以太网更高的优先级?

EN

Stack Overflow用户

回答已采纳

发布于 2021-12-08 18:26:26

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

选项1:更新AOSP源代码

在Android9中,ConnectivityService将使用静态网络评分来确定使用各种网络传输类型(以太网与will与蜂窝等)的优先级。

以太网的网络得分为70 (链接),Wi的网络得分为60 (链接)。

在你的例子中,如果我想把Wi放在以太网之上的优先级,你可以改变评分以反映新的优先级(例如,在Wi链路上将Wi更改为71,或者类似地,下以太网在其工厂中的得分为59 )。

例如Wi:

代码语言:javascript
运行
复制
private static final int SCORE_FILTER = 71;

选项2:使用资源覆盖

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

代码语言:javascript
运行
复制
<!-- 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>

这是建立在以太网接口名称上的索引,所以在所有情况下都需要有相同的以太网名称,例如大多数人使用的eth0。您可以更新上面的配置来使用这些功能。在您的示例中,您可以省略NOT_RESTRICTED功能(链接),在这种情况下,以太网永远不会被用作默认网络,只会留下Wi优先级更高的问题。

代码语言:javascript
运行
复制
  <!-- Restricting eth0 -->
  <string-array translatable="false" name="config_ethernet_interfaces">
    <item>eth0;11,12,14;;</item>
  </string-array>

事实上,Android目标(link)今天也是出于类似的原因而这么做的。请注意,上面的配置将eth0标记为受限,因此它可能不会获得IP或完全不被使用(除非有可能被限制网络访问的应用程序使用)。如果你想要一种不同的行为,你可以随时利用这些功能。

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70260615

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档