反射设置android6.0 以太网的打开和设置静态Ip,不用导源码包

话不多说,直接上代码,代码注释有说明,还是看不懂的建议自己查看

sdk路径\sources\android-23\android\net下的相关文件,相信大家会有所收获的:

try {
            //获取ETHERNET_SERVICE参数
            String ETHERNET_SERVICE = (String) Context.class.getField("ETHERNET_SERVICE").get(null);
			
            Class<?> ethernetManagerClass = Class.forName("android.net.EthernetManager");
			
            Class<?> ipConfigurationClass = Class.forName("android.net.IpConfiguration");
			
            //获取ethernetManager服务对象
            Object ethernetManager = getSystemService(ETHERNET_SERVICE);
			
            Object getConfiguration = ethernetManagerClass.getDeclaredMethod("getConfiguration").invoke(ethernetManager);
			
            Log.e(TAG, "ETHERNET_SERVICE : "+ ETHERNET_SERVICE);
			
            //获取在EthernetManager中的抽象类mService成员变量
            Field mService = ethernetManagerClass.getDeclaredField("mService");
			
            //修改private权限
            mService.setAccessible(true);
			
            //获取抽象类的实例化对象
            Object mServiceObject = mService.get(ethernetManager);
			
            Class<?> iEthernetManagerClass = Class.forName("android.net.IEthernetManager");
			
            Method[] methods = iEthernetManagerClass.getDeclaredMethods();
			
            for (Method ms : methods) {
			
                if (ms.getName().equals("setEthernetEnabled")){
				
                    ms.invoke(mServiceObject,true);
					
                    Log.e(TAG, "mServiceObject : "+mServiceObject);
					
                }
				
            }
            Class<?> stati
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 25
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值