android 以太网(ethernet)设置静态信息和动态信息.IP.子网掩码.getaway(以太网 二)

上篇介绍了以太网信息获取,这篇介绍以太网信息获取,这篇直接贴设置代码

上篇地址:https://blog.csdn.net/qq_35345103/article/details/84573021

//设置静态IP信息 
public void processIpSettings(String etherentip,String etherentmasks,
                                  String etherentgeteway,String etherentdns) {

        IpConfiguration ipConfiguration = getConfiguration();

        ipConfiguration.setIpAssignment(IpConfiguration.IpAssignment.STATIC);
        ipConfiguration.setProxySettings(ipConfiguration.getProxySettings());

        StaticIpConfiguration staticConfig = new StaticIpConfiguration();
        Inet4Address inetAddr = null;
        try {
            inetAddr = (Inet4Address) NetworkUtils.numericToInetAddress(etherentip);
        } catch (IllegalArgumentException|ClassCastException e) {

        }

        int wasks = util.twoget(etherentmasks);
        try {
            staticConfig.ipAddress = new LinkAddress(inetAddr, wasks);
        } catch (NumberFormatException e) {

        }

        if (!TextUtils.isEmpty(etherentgeteway)) {
            try {
                staticConfig.gateway =(Inet4Address) NetworkUtils.numericToInetAddress(etherentgeteway);
            } catch (IllegalArgumentException|ClassCastException e) {
            }
        }

        if (!TextUtils.isEmpty(etherentdns)) {

            try {
                staticConfig.dnsServers.add(
                        (Inet4Address) NetworkUtils.numericToInetAddress(etherentdns));
            } catch (IllegalArgumentException|ClassCastException e) {

            }
        }
        ipConfiguration.setStaticIpConfiguration(staticConfig);
          getmEthernetManager().setConfiguration(ipConfiguration);
    }
//设置为动态IP
public void setEthernetDhcp(){
        IpConfiguration ipConfiguration = getConfiguration();
        if(getethernetstatic()) {
            ipConfiguration.setIpAssignment(IpConfiguration.IpAssignment.DHCP);
            mEthernetManager.setConfiguration(ipConfiguration);
        }
    }
   //获取以太网服务
 public EthernetManager  getmEthernetManager(){
      mEthernetManager = (EthernetManager) context.getSystemService(Context.ETHERNET_SERVICE);
        return mEthernetManager;
    }

//解析子网掩码为数字

public  int twoget( String netwak){
        int wak = 0;

        if(netwak==null){
            return 24;
        }
        StringBuffer netmask = new StringBuffer();
        String[] ipArray = netwak.split("\\.");

        for (int j = 0; j < ipArray.length; j++) {
            String s = ipArray[j];
            String a=Integer.toBinaryString(Integer.valueOf(s));
            String aString2[]=a.split("");
            for (int i = 0; i < aString2.length; i++) {
                if ("1".equals(aString2[i])) {
                    wak++;
                }
            }
        }
        if(wak<=0 && wak>32){
            return 24;
        }

        return wak;


    }

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值