为usb网卡设置ip地址之三

    private boolean setRoute(String route) {
        try{
            IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
            INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
            boolean ret = service.replaceV4DefaultRoute(ETH0,route) ;
            return ret;
        } catch (android.os.RemoteException e) {
            Log.w(TAG,"repalceV4DefaultRoute=" + e);
            return false;
        }
    }
    private  int computeprefixLength(InetAddress mask) {
        int count = 0;
        for (byte b : mask.getAddress()) {
            for (int i = 0; i < 8; ++i) {
                if ((b & (1 << i)) != 0) {
                    ++count;
                }
            }
        }
        return count;
    }
    
    private boolean isValideFromat(String address) {
        if (address == null) {
            return false;
        }
        String ipreg ="^(([1-9]{1}[0-9]{1}|[0-9]{1}|1\\d\\d|2[0-4]\\d|25[0-5])(\\.)){3}([1-9]{1}[0-9]{1}|[0-9]{1}|1\\d\\d|2[0-4]\\d|25[0-5])$"; 
        Pattern p = Pattern.compile(ipreg);
        Matcher m = p.matcher(address);
        if (m.find()) {
            return true;
        } else {
            return false;
        }
    }
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if ("android.hardware.usb.action.USB_DEVICE_DETACHED".equals(action)) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                Log.i(TAG,"USB_DEVICE_DETACHED device="+device);
                
                ipEditText.setEnabled(false);
                netmastEditText.setEnabled(false);
                gwEditText.setEnabled(false);
                dnsEditText.setEnabled(false);
                okBtn.setEnabled(false);
                
                //usbMacAddrmodePreference.setChecked(false); 
            }  
        }
    };

你可能感兴趣的:(IP地址)