苦心鉆研6天以及各路朋友的幫助下終于有了成果,安卓6.0系統重啟后也可以修改有關有線網的IP,網關地址等。
在這里分享出來,幫助更多人少走彎路。
不羅嗦切入正題,實現以太網的IP修改主要分為以下步驟:
一.拿到系統的Framework源碼,也就是jar包(目的調用系統的隱藏方法)
我的jar是底層開發(fā)的同事給我的,導入到項目中,導入方式可參考這個帖子:
http://www.itdecent.cn/p/ccda0c6938ea
二.調用系統源碼,修改信息
(1)聲明變量:
EthernetManager mEthManager;
privatestatic String mEthIpAddress = "192.168.253.247";//IP
privatestatic String mEthNetmask = "255.255.255.0";// ?子網掩碼
privatestatic String mEthGateway = "192.168.88.1";//網關
privatestatic String mEthdns1 = "8.8.8.8";// DNS1
privatestatic String mEthdns2 = "8.8.4.4";// DNS2
(2)在需要改變信息的地方加上如下代碼:
mEthManager = (EthernetManager)getSystemService("ethernet");
InetAddress ipaddr=NetworkUtils.numericToInetAddress(mEthIpAddress);
DhcpInfo dhcpInfo = new DhcpInfo();
dhcpInfo.ipAddress = inetAddressToInt(ipaddr);
Inet4Address inetAddr= Utils.getIPv4Address(mEthIpAddress);
int prefixLength=Utils.maskStr2InetMask(mEthNetmask);
InetAddress gatewayAddr = Utils.getIPv4Address(mEthGateway);
InetAddress dnsAddr = Utils.getIPv4Address(mEthdns1);
if (inetAddr.getAddress().toString().isEmpty() || prefixLength ==0 || gatewayAddr.toString().isEmpty()
|| dnsAddr.toString().isEmpty()) {
return;
}
Class clazz = null;
try {
clazz = Class.forName("android.net.LinkAddress");
} catch (Exception e) {
// TODO: handle exception
}
Class[] cl = new Class[]{InetAddress.class, int.class};
Constructor cons = null;
try {
cons = clazz.getConstructor(cl);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Object[] x = {inetAddr, prefixLength};
StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration();
String dnsStr2 = mEthdns2;
//mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength);
try {
mStaticIpConfiguration.ipAddress = (LinkAddress) cons.newInstance(x);
Log.d("232323", "chanson 1111111");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
mStaticIpConfiguration.gateway=gatewayAddr;
mStaticIpConfiguration.dnsServers.add(dnsAddr);
if (!dnsStr2.isEmpty()) {
mStaticIpConfiguration.dnsServers.add(Utils.getIPv4Address(dnsStr2));
}
Log.d("2312321", "chanson mStaticIpConfiguration ?====" + mStaticIpConfiguration);
IpConfiguration ?mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC,
IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);
mEthManager.setConfiguration(mIpConfiguration);
Utils類相關方法:
public static Inet4Address getIPv4Address(String text) {
try {
return (Inet4Address) NetworkUtils.numericToInetAddress(text);
} catch (IllegalArgumentException|ClassCastException e) {
return null;
}
}
public static int maskStr2InetMask(String maskStr) {
StringBuffer sb ;
String str;
int inetmask = 0;
int count = 0;
/*
* check the subMask format
*/
Pattern pattern = Pattern.compile("(^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$");
if (pattern.matcher(maskStr).matches() == false) {
Log.e("33333","subMask is error");
return 0;
}
String[] ipSegment = maskStr.split("\\.");
for(int n =0; n
sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n])));
str = sb.reverse().toString();
count=0;
for(int i=0; i
i=str.indexOf("1",i);
if(i==-1)
break;
count++;
}
inetmask+=count;
}
return inetmask;
}
三.簽名,打包(目的為了獲取系統權限)
(1)在桌面新建文件夾,放入以下文件:
(2)編輯signapk.bat文件:
(3)雙擊signapk.bat,會在新建文件夾下看到new.apk,裝到手機看效果即可。
至此所有步驟完成,如果有寫的不好的地方請見諒,有錯誤請指正批評,有更好的實現方式請留言,謝謝支持。
參考帖子:http://www.itdecent.cn/p/e1191c41d70a
補充:修改以太網,網絡連接模式(動態(tài)獲取或靜態(tài))
找到代碼: IpConfiguration? mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);
第一個參數,點擊進入可以看到有三個選項:STATIC,DHCP,UNASSIGNED;選擇STATIC即靜態(tài)鏈接,DHCP即動態(tài)獲取。
QQ群:Android開發(fā)交流243624914