設置靜態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); ArrayListmDnsServers = 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 diff diff --git a/java/com/android/server/ethernet/EthernetNetworkFactory.java b/java/com/android/server/ethernet/EthernetNetworkFactory.java index 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); + } }