阅读更多

設置靜態IP的方法如下:

   
... mEthManager = (EthernetManager) mContext.getSystemService(Context.ETHERNET_SERVICE);...private void setEthernetConfig(){        try {            StaticIpConfiguration staticIpConfiguration = new StaticIpConfiguration();            InetAddress mIpAddr = NetworkUtils.numericToInetAddress(tIpAddressString);            LinkAddress mIpAddress = new LinkAddress(mIpAddr,tNetworkPrefixLength);            InetAddress mGateway = NetworkUtils.numericToInetAddress(tGatewayString);            ArrayList mDnsServers = new ArrayList();            mDnsServers.add(NetworkUtils.numericToInetAddress(tDnsFirstString));            mDnsServers.add(NetworkUtils.numericToInetAddress(tDnsSecondString));            staticIpConfiguration.ipAddress = mIpAddress;            staticIpConfiguration.gateway = mGateway;            staticIpConfiguration.dnsServers.addAll(mDnsServers);            if(mConnectionTypeSelect.contains(CONNECT_TYPE_STATIC)) {                if (mStaticProxySelect.contains(PROXY_NONE)) {                    config = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, staticIpConfiguration, null);                } else if (mStaticProxySelect.contains(PROXY_STATIC)) {                    config = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.STATIC, staticIpConfiguration, ProxyInfo.buildDirectProxy(null, 0));                } else if (mStaticProxySelect.contains(PROXY_PAC)) {                    config = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.PAC, staticIpConfiguration, ProxyInfo.buildDirectProxy(null, 0));                } else if (mStaticProxySelect.contains(PROXY_UNASSIGNED)) {                    config = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.UNASSIGNED, staticIpConfiguration, ProxyInfo.buildDirectProxy(null, 0));                } else {                    config = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, staticIpConfiguration, null);                }            }else{                config = new IpConfiguration(IpConfiguration.IpAssignment.DHCP, IpConfiguration.ProxySettings.NONE, null, ProxyInfo.buildDirectProxy(null,0));            }            mEthManager.setConfiguration(config);        }catch (Exception e){            e.printStackTrace();        }        Settings.System.putString(mContext.getContentResolver(),                Settings.System.KEY_ETHERNET_CONNECTION_TYPE, mConnectionTypeSelect);        Settings.System.putString(mContext.getContentResolver(),                Settings.System.KEY_ETHERNET_STATIC_IP, tIpAddressString);        Settings.System.putString(mContext.getContentResolver(),                Settings.System.KEY_ETHERNET_STATIC_GATEWAY, tGatewayString);        Settings.System.putString(mContext.getContentResolver(),                Settings.System.KEY_ETHERNET_STATIC_SUBMASK, tSubMaskString);        Settings.System.putString(mContext.getContentResolver(),                Settings.System.KEY_ETHERNET_STATIC_DNS_FIRST, tDnsFirstString);        Settings.System.putString(mContext.getContentResolver(),                Settings.System.KEY_ETHERNET_STATIC_DNS_SECOND, tDnsSecondString);        Settings.System.putString(mContext.getContentResolver(),                Settings.System.KEY_ETHERNET_STATIC_PROXY, mStaticProxySelect);    }
 

 設置好之後需要在/frameworks/opt/net/ethernet/java/com/android/server/ethernet/EthernetNetworkFactory.java 中做如下處理[不然會出現ifconfig 可以看到設置的static IP,但是不能上網,設置中也不能看到IP]

 

leif@leif:~/Git/Arashi9500/LA.UM.6.2/LINUX/android/frameworks/opt/net/ethernet$ git diffdiff --git a/java/com/android/server/ethernet/EthernetNetworkFactory.java b/java/com/android/server/ethernet/EthernetNetworkFactory.javaindex d786b4a..308e183 100644--- a/java/com/android/server/ethernet/EthernetNetworkFactory.java+++ b/java/com/android/server/ethernet/EthernetNetworkFactory.java@@ -349,44 +349,60 @@ class EthernetNetworkFactory {                 return;             }             linkProperties = config.getStaticIpConfiguration().toLinkProperties(mIface);-        } else {-            mNetworkInfo.setDetailedState(DetailedState.OBTAINING_IPADDR, null, mHwAddr);-            IpManager.Callback ipmCallback = new IpManager.Callback() {-                @Override-                public void onProvisioningSuccess(LinkProperties newLp) {-                    mHandler.post(() -> onIpLayerStarted(newLp));-                }--                @Override-                public void onProvisioningFailure(LinkProperties newLp) {-                    mHandler.post(() -> onIpLayerStopped(newLp));-                }--                @Override-                public void onLinkPropertiesChange(LinkProperties newLp) {-                    mHandler.post(() -> updateLinkProperties(newLp));-                }-            };--            stopIpManager();-            mIpManager = new IpManager(mContext, mIface, ipmCallback);              if (config.getProxySettings() == ProxySettings.STATIC ||                     config.getProxySettings() == ProxySettings.PAC) {-                mIpManager.setHttpProxy(config.getHttpProxy());+                linkProperties.setHttpProxy(config.getHttpProxy());             } -            final String tcpBufferSizes = mContext.getResources().getString(+            String tcpBufferSizes = mContext.getResources().getString(                     com.android.internal.R.string.config_ethernet_tcp_buffers);-            if (!TextUtils.isEmpty(tcpBufferSizes)) {-                mIpManager.setTcpBufferSizes(tcpBufferSizes);+            if (TextUtils.isEmpty(tcpBufferSizes) == false) {+                linkProperties.setTcpBufferSizes(tcpBufferSizes);+            }+        } else {+            mNetworkInfo.setDetailedState(DetailedState.OBTAINING_IPADDR, null, mHwAddr);+        }+        IpManager.Callback ipmCallback = new IpManager.Callback() {+            @Override+            public void onProvisioningSuccess(LinkProperties newLp) {+                mHandler.post(() -> onIpLayerStarted(newLp));             } +            @Override+            public void onProvisioningFailure(LinkProperties newLp) {+                mHandler.post(() -> onIpLayerStopped(newLp));+            }++            @Override+            public void onLinkPropertiesChange(LinkProperties newLp) {+                mHandler.post(() -> updateLinkProperties(newLp));+            }+        };++        stopIpManager();+        mIpManager = new IpManager(mContext, mIface, ipmCallback);++        if (config.getProxySettings() == ProxySettings.STATIC ||+                config.getProxySettings() == ProxySettings.PAC) {+            mIpManager.setHttpProxy(config.getHttpProxy());+        }++        final String tcpBufferSizes = mContext.getResources().getString(+                com.android.internal.R.string.config_ethernet_tcp_buffers);+        if (!TextUtils.isEmpty(tcpBufferSizes)) {+            mIpManager.setTcpBufferSizes(tcpBufferSizes);+        }++        if (config.getIpAssignment() == IpAssignment.STATIC) {+            mIpManager.startProvisioning(config.getStaticIpConfiguration());+        } else {             final ProvisioningConfiguration provisioningConfiguration =                     mIpManager.buildProvisioningConfiguration()                             .withProvisioningTimeoutMs(0)                             .build();             mIpManager.startProvisioning(provisioningConfiguration);+         }     }

 

更多相关文章

  1. Fragment 在Android(安卓)SDK1.6上实现
  2. Android(安卓)Bundle类
  3. android中设置一些没有maxHeight属性控件的最高值
  4. 判断Android手机是否联网
  5. android手机端与PC端使用adb forword通信
  6. android sqlite java.lang.IllegalArgumentException: contains
  7. 如何使用ndk中addr2line工具查询so库中错误信息行数
  8. Android——使用 Broastcast 实现进程间通讯
  9. android "Only the original thread that created a view hierar

随机推荐

  1. 多厂商***系列之三:Cisco EZ***的实现【PC
  2. 安全星球企业云盘:释放数据价值,推动企业
  3. 详解javascript的bind方法
  4. 身份和访问管理(IAM)
  5. Python再学习之列表介绍
  6. 如何使用 Apple Watch 控制 Mac 或 PC 上
  7. 搭建kerberos高可用集群
  8. 小技巧 | 用python给敏感信息加水印
  9. 电脑读不出移动硬盘的原因和解决办法
  10. 实战 | Pinpoint全链路监控搭建