好友
阅读权限20
听众
最后登录1970-1-1
|
可以参考https://blog.csdn.net/weixin_41722842/article/details/82770904 AndroidThings设置以太网卡静态ip
也可以用反射的方式来修改:
public static boolean setEthernetStaticIp(Context context, String address, String mask, String gate, String dns) {
try {
@SuppressLint("PrivateApi") Class<?> ethernetManagerCls = Class.forName("android.net.EthernetManager");
//获取EthernetManager实例
@SuppressLint("WrongConstant") Object ethManager = context.getSystemService("ethernet");
//创建StaticIpConfiguration
Object staticIpConfiguration = newStaticIpConfiguration(address, gate, mask, dns);
//创建IpConfiguration
Object ipConfiguration = newIpConfiguration(staticIpConfiguration);
//获取EthernetManager的setConfiguration()
Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", String.class,ipConfiguration.getClass());
//保存静态ip设置
saveIpSettings(context, address, mask, gate, dns);
//设置静态IP
setConfigurationMethod.invoke(ethManager,"eth0", ipConfiguration);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
} |
|