Android6.0修改以太网IP

苦心钻研6天以及各路朋友的帮助下终于有了成果,安卓6.0系统重启后也可以修改有关有线网的IP,网关地址等。

在这里分享出来,帮助更多人少走弯路。

不罗嗦切入正题,实现以太网的IP修改主要分为以下步骤:

一.拿到系统的Framework源码,也就是jar包(目的调用系统的隐藏方法)

我的jar是底层开发的同事给我的,导入到项目中,导入方式可参考这个帖子:

http://www.jianshu.com/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)在桌面新建文件夹,放入以下文件:

Android6.0修改以太网IP_第1张图片

(2)编辑signapk.bat文件:

Android6.0修改以太网IP_第2张图片

(3)双击signapk.bat,会在新建文件夹下看到new.apk,装到手机看效果即可。

至此所有步骤完成,如果有写的不好的地方请见谅,有错误请指正批评,有更好的实现方式请留言,谢谢支持。

参考帖子:http://www.jianshu.com/p/e1191c41d70a


补充:修改以太网,网络连接模式(动态获取或静态)

找到代码: IpConfiguration  mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);

第一个参数,点击进入可以看到有三个选项:STATIC,DHCP,UNASSIGNED;选择STATIC即静态链接,DHCP即动态获取。


QQ群:Android开发交流243624914

你可能感兴趣的:(Android6.0修改以太网IP)