Android6.0修改以太网IP

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

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

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

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

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

  http://www.jianshu.com/p/ccda0c6938ea

二.调用系统源码,修改信息

    (1)声明变量:

    EthernetManager mEthManager;
    private  static String mEthIpAddress = "192.168.253.247";  //IP
    private  static String mEthNetmask = "255.255.255.0";  //  子网掩码
    private  static String mEthGateway = "192.168.88.1";   //网关
    private  static String mEthdns1 = "8.8.8.8";   // DNS1
    private  static 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<ipSegment.length;n++) {
            sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n])));
            str = sb.reverse().toString();
            count=0;
            for(int i=0; i<str.length();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.jianshu.com/p/e1191c41d70a

补充:修改以太网,网络连接模式(动态获取或静态)
找到代码: IpConfiguration  mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);

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

最新补充:最后打包的时候,放到文件夹的apk要先用AS进行打包,然后再把打包生成的apk放进去,生成系统级apk。

QQ群:Android开发交流  243624914



  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值