话不多说,直接上代码,代码注释有说明,还是看不懂的建议自己查看
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