設置靜態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<InetAddress> mDnsServers = new ArrayList<InetAddress>(); 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); + } }
相关推荐
Android 8.0 Broadcast 静态注册自动转为动态注册
这个是api 21~25(android 5.0.1-7.1.1)能用的app源码,使用eclipse+adt开发(不习惯用AS),android8.0(api26)因时间原因未能测试是否可用。不过android10.0因为api变化被证明肯定不能用。里面除了源码、还有...
Android 8.0的NotoColorEmoji.ttf文件,push进自己android的/system/fonts目录替换,可以使手机显示Android 8.0风格的emoji。 注意:解压后需要修改修改文件名为NotoColorEmoji.ttf,并且记得备份原文件,以备...
anroid8.0 SDK包,编译android8.0必备工具,移植android8.0必备工具
android8.0的api,很有用的代码编写感觉,会让你的编译速度加快
android8.0-API26.CHM中文版chmandroid8.0-API26.CHM中文版chmandroid8.0-android8.0-API26.CHM中文版chmAPI26.CHM中文版chmandroid8.0-API26.CHM中文版chm
Android 8.0 系统自带 Settings Android 8.0 系统自带 Settings
Android8.0 申请更新APP,或者分享文件
Android 8.0 Launcher开发指南.
Android 8.0指纹验证,前提必须已经设置了指纹信息,才能进行操作,代码里面都有验证,欢迎下载
Android 8.0 Launcher3 的源代码 ,修改了系统原有的Launcher3的自定义权限冲突。
android 8.0 对 通知栏有了很大的改变,以往的方法写通知栏会报错,google新加了一个notificationChannel,而且必须添加,这里是Demo。
android 8.0系统应用图标适配步骤:1.新建一个android项目2.新建的android项目AndroidManifest.xml里面android:
Android8.0 Launcher2源码,基于Android Studio工程源码,亲测可以在手机上运行。
Android8.0画中画(pip)完整DEMO,无废话。看不懂官方文档的同学来,下载即可运行。
Android 8.0API概览,详细描述了Android 8.0API, Android 8.0API概览,详细描述了Android 8.0API
Android8.0 API26 (谷歌翻译版+原版) 顺便打个包
Android8.0之前 未知应用安装权限默认开启,如下图所示 8.0以下手机未知应用安装权限.png Android8.0之后 未知应用安装权限默认关闭,且权限入口隐藏。 如何开启未知应用安装权限的入口,并设置允许安装? 在...
Android8.0设计的初衷是允许合作方可以独立的更新他们自有的策略,即在Android8.0之后Google允许合作方将自有的策略部分与系统原有的进行分离,如合作方可以将自有的部分解耦到vendor.img分区中,在需要更新的情况下...
SDK Android 8.0 (API 26) SDK Android 8.0 (API 26) SDK Android 8.0 (API 26)