iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android 13 以太网开发总结
  • 571
分享到

Android 13 以太网开发总结

androidjava网络 2023-09-27 21:09:29 571人浏览 独家记忆
摘要

Android 13 以太网开发总结 前言 相较于Android12,Android13将以太网相关功能整合到ConnectivityService里,将以太网的核心源码从framework上移到pa

Android 13 以太网开发总结

前言

相较于Android12,Android13将以太网相关功能整合到ConnectivityService里,将以太网的核心源码从framework上移到packages/modules/Connectivity下,功能也做了相关更新。


一、具体变更

对于Android13以前的版本的方法做了限制,不允许Android13以上的版本使用。如设置以太网参数的方法和获取以太网参数的方法。

增加了以太网开关的方法setEthernetEnabled()。

新增了车机项目更新以太网参数的方法updateConfiguration()。

新增以太网状态变更通知机制。

二、合入mainline后以太网开发思路

1、由于合入mainline后,ConnectivityService会被替换,以前开发使用的在EthernetService里实现新接口的方法行不通了。

2、如何获取以太网状态变更:

以前是通过判断以太网节点eth0是否存在来判断以太网是否连接,在Android13上是行不通的。但是通过阅读源码发现,Android13版本的以太网帮你实现了以太网状态变更后通知的机制,当EthernetService处理完以太网插拔事件、网络配置更新事件、以太网开启关闭事件后会主动调用
InterfaceStateListener通知所有监听。
通过实现他的接口就可监听以太网状态的变更,可以监听到以太网的状态state、路由role、网络配置IpConfiguration 。

    public interface InterfaceStateListener {                @Systemapi(client = MODULE_LIBRARIES)        void onInterfaceStateChanged(@NonNull String iface, @InterfaceState int state,                @Role int role, @Nullable IpConfiguration configuration);    }     mEthernetListener = new EthernetManager.InterfaceStateListener() {         @Override         public void onInterfaceStateChanged(@NonNull String iface, int state, int role,@NonNull IpConfiguration configuration) {                 //do somthing         }     };

3、如何调用已经禁止使用的方法:getConfiguration() setConfiguration()

Google通过

@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)

来禁止不符合条件的app和service来调用它的方法。简单来说就是如果api高于Android11 ,应用就无法使用以前的接口设置有线网配置。

如果可以修改源码,当然有很多方法来越过这些限制,比如实现一个名字不同功能一样的接口,这样既不影响CTS测试,又可以轻松的完成工作。但是上文所说Android13以太网相关功能被整合到ConnectivityService里,而mainline会替换ConnectivityService,导致无法修改源码。

为了绕开限制,作者也做过很多尝试,但都行不通。Android这几年框架变化很快,越来越符合我们刚学习编程时老师教我们的那句思想:”高内聚,低耦合“,也就是模块化越来越明显了,深入学习Android编译规则在工作中也会更加轻松。好了,诸多方法行不通后,我们还有最后的大招:“反射”,亲测反射还是可以正常调用这些做了限制的方法。

    private IpConfiguration getConfiguration(String iface){        IpConfiguration mIpConfiguration = null;        try{            Class<? extends EthernetManager> c = mEthManager.getClass();            Method method = c.getMethod("getConfiguration", String.class);            EthernetManager tempManager = mEthManager;            method.setAccessible(true);            Log.e(TAG,"get getConfiguration Method: " + (method == null));            Object Values = method.invoke(tempManager,iface);            mIpConfiguration = (IpConfiguration)Values;        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {            Log.e(TAG,"getDeclaredMethod: " + e.getMessage());        }        return mIpConfiguration;    }
    private void setConfiguration(String iface ,IpConfiguration config){        try{            Class<? extends EthernetManager> c = mEthManager.getClass();            Method method = c.getMethod("setConfiguration", String.class ,IpConfiguration.class);            EthernetManager tempManager = mEthManager;            method.setAccessible(true);            Log.e(TAG,"get setConfiguration Method: " + (method == null));            method.invoke(tempManager,iface, config);        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {            Log.e(TAG,"getDeclaredMethod: " + e.getMessage());        }    }

总结

例如:以上就是今天要讲的内容,个人见解,如有不对请指出。

来源地址:https://blog.csdn.net/qq_42457766/article/details/131069774

--结束END--

本文标题: Android 13 以太网开发总结

本文链接: https://www.lsjlt.com/news/420129.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作