今天说下修改以太网ip的方法,方便大家也方便自己以后查询。关键代码
ConnectivityManager mService = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
EthernetManager mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE);
if(mEthManager.getState() == EthernetManager.ETHERNET_STATE_ENABLED) {
EthernetDevInfo mInterfaceInfo = mEthManager.getSavedConfig();
String mIp;
String mMask;
String mGw;
String mDns;
mIp = "192.168.0.118";
mMask = "255.255.255.0";
mGw = "192.168.0.1";
mDns = "192.168.0.1";
mInterfaceInfo.setConnectMode(EthernetDevInfo.ETHERNET_CONN_MODE_MANUAL);
mInterfaceInfo.setIpAddress(mIp);
mInterfaceInfo.setNetMask(mMask);
mInterfaceInfo.setDnsAddr(mDns);
mInterfaceInfo.setGateWay(mGw);
try{
mEthManager.updateDevInfo(mInterfaceInfo);
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
} else {
Toast.makeText(this, "Ethernet state disabled!", 5000).show();
}
测试随便写的,已经验证。大家根据需要自己改吧。(需要源码环境编译)
ConnectivityManager mService = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
EthernetManager mEthManager = (EthernetManager) getSystemService(Context.ETHERNET_SERVICE);
if(mEthManager.getState() == EthernetManager.ETHERNET_STATE_ENABLED) {
EthernetDevInfo mInterfaceInfo = mEthManager.getSavedConfig();
String mIp;
String mMask;
String mGw;
String mDns;
mIp = "192.168.0.118";
mMask = "255.255.255.0";
mGw = "192.168.0.1";
mDns = "192.168.0.1";
mInterfaceInfo.setConnectMode(EthernetDevInfo.ETHERNET_CONN_MODE_MANUAL);
mInterfaceInfo.setIpAddress(mIp);
mInterfaceInfo.setNetMask(mMask);
mInterfaceInfo.setDnsAddr(mDns);
mInterfaceInfo.setGateWay(mGw);
try{
mEthManager.updateDevInfo(mInterfaceInfo);
Thread.sleep(500);
}catch(Exception e){
e.printStackTrace();
}
} else {
Toast.makeText(this, "Ethernet state disabled!", 5000).show();
}
测试随便写的,已经验证。大家根据需要自己改吧。(需要源码环境编译)