android 8.0 設置Ethernet的靜態IP
16lz
2021-01-23
阅读更多
設置靜態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 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);+ } }
更多相关文章
- android中的一个属性动画,可以显示更多的一个案例
- Android下拉刷新和上拉加载更多
- 实现Android ListView 自动加载更多内容
- Android studio 下拉刷新,加载更多使用LoadingViewFinal
- Android 下拉加载更多 上拉刷新 框架 (太极 八卦样式刷新,支持自定
- andorid RecyclerView下拉刷新,上拉加载更多
- OpenGL ES教程V之更多3D模型(原文对照)
- Android从零撸美团(四) - 美团首页布局解析及实现 - Banner+自定
- EditText设置更多文字为省略号