图书馆网站建设的建议惠州百度网络推广
web/
2025/10/7 8:19:12/
文章来源:
图书馆网站建设的建议,惠州百度网络推广,app设计公司,办公室设计方案Android静态ip设置的坑
Android静态ip设置#xff0c;对于这个功能#xff0c;如果没有接触过#xff0c;会给人感觉是个特别简单的功能#xff0c;直接调用系统的接口即可#xff0c;其实这个功能还是有许多坑的#xff0c;因为谷歌在Android SDK中对相关的API进行非系…Android静态ip设置的坑
Android静态ip设置对于这个功能如果没有接触过会给人感觉是个特别简单的功能直接调用系统的接口即可其实这个功能还是有许多坑的因为谷歌在Android SDK中对相关的API进行非系统层的隐藏打上了hide标签使得上层无法直接调用现有的接口其中隐藏的API包含EthernetManager,IpConfiguration,StaticIpConfiguration等接口其中有的API虽然提供但是关键的接口却被隐藏了。导致本来简单功能变得无比繁琐因此本文就对静态ip设置的坑做个总结为相关开发者提供一个简易的参考。 解决Android静态ip设置的问题可以从两个方面着手
一方面有系统层接口的支持
系统接口也就是直接由系统层提供接口一般是framework jar包上层直接调用接口传参但是需要调整Gradle中的SDK的加载顺序保证framework包正确替换AndroidSDK的jar包否则编译会报错。
另一方面就是App层自己使用java的动态机制
动态机制即java的反射通过反射调用系统隐藏的接口但是这种方式发现在Android6以下的版本是可以的Android6以上的版本虽然设置成功设置里面参数也改了但是重启你会发现已经设置的静态模式会自动切换为动态模式即系统不做数据存储保存只是零时保存在内存中所以你重启以后就被系统还原了参考GitHub分析是高版本谷歌拦截了反射机制不让非系统层修改数据而且这个拦截是在C层进行的特别恶心你绕都绕不过去,不过如果你的应用是Launcher应用的话可以在App层做取巧就是记录用户重启前是否做静态ip的保存如果用户操作了静态ip保存那么App启动的时候在Application的onCreate方法中下发静态ip配置保证重启前静态ip状态和重启后的状态一致就能解决这个问题。以下就静态ip设置从系统层App层两个方面做简要的代码实现分析
1.系统层的实现就是系统提供的接口直接调用思路是先创建EthernetManager对象接着创建StaticIpConfiguration对象再使用StaticIpConfiguration对象创建IpConfiguration对象最后将IpConfiguration设置给EthernetManager中即可代码如下
/*** 项目名称demo* 描述* 静态ip的配置通过framework.jar直调隐藏的系统接口*/
public class FEthernetUtil {private static EthernetManager mEthManager;private static StaticIpConfiguration mStaticIpConfiguration;private static IpConfiguration mIpConfiguration;/*** 描述* param context* param iFace //网口名称* param ip * param gateway* param netmask* param dns1* param dns2* return*/SuppressLint(WrongConstant)public static int setStaticIp(Context context, String iFace, String ip, String gateway, String netmask, String dns1, String dns2) {//获取EthernetManager对象mEthManager (EthernetManager) context.getSystemService(ethernet);if (setStaticIpConfiguration(ip, gateway, netmask, dns1, dns2)) {mEthManager.setConfiguration(iFace, mIpConfiguration);return 0;}return 1;}/*** 描述 初始mIpConfiguration* param ip * param gateway* param netmask* param dns1* param dns2**/private static boolean setStaticIpConfiguration(String ip, String gateway, String netmask, String dns1, String dns2) {Inet4Address inetAddr (Inet4Address) InetAddresses.parseNumericAddress(ip);int prefixLength maskStr2InetMask(netmask);InetAddress gatewayAddr InetAddresses.parseNumericAddress(gateway);InetAddress dnsAddr InetAddresses.parseNumericAddress(dns1);if (null inetAddr || inetAddr.getAddress().toString().isEmpty() || prefixLength 0 || gatewayAddr.toString().isEmpty() || dnsAddr.toString().isEmpty()) {return false;}String dnsStr2 dns2;ArrayListInetAddress dnsAddrs new ArrayListInetAddress();dnsAddrs.add(dnsAddr);if (!dnsStr2.isEmpty()) {dnsAddrs.add(InetAddresses.parseNumericAddress(dns2));}mStaticIpConfiguration new StaticIpConfiguration.Builder().setIpAddress(new LinkAddress(inetAddr, prefixLength)).setGateway(gatewayAddr).setDnsServers(dnsAddrs).build();mIpConfiguration new IpConfiguration();mIpConfiguration.setIpAssignment(IpConfiguration.IpAssignment.STATIC);mIpConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE);mIpConfiguration.setStaticIpConfiguration(mStaticIpConfiguration);return true;}/*** 描述* 子网掩码解析* param maskStr* return int**/private static int maskStr2InetMask(String maskStr) {StringBuffer sb;String str;int inetmask 0;int count 0;/** check the subMask format*/Pattern pattern Pattern.compile((^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$);if (pattern.matcher(maskStr).matches() false) {return 0;}String[] ipSegment maskStr.split(\\.);for (String s : ipSegment) {sb new StringBuffer(Integer.toBinaryString(Integer.parseInt(s)));str sb.reverse().toString();count 0;for (int i 0; i str.length(); i) {i str.indexOf(1, i);if (i -1) break;count;}inetmask count;}return inetmask;}
}拿到系统层的jar包跟使用其他jar一样进行gradle依赖配置需要注意是gradle中的jar包加载顺序修改
plugins {id com.android.libraryid org.jetbrains.kotlin.android
}......
dependencies {implementation androidx.core:core-ktx:1.8.0api(files(libs/framework.jar))//系统层配置静态ip的jar包
}gradle.projectsEvaluated {tasks.withType(JavaCompile) {SetFile fileSet options.bootstrapClasspath.getFiles();ListFile newFileList new ArrayList()newFileList.add(new File(libs/framework.jar))//加载本地系统jar包newFileList.addAll(fileSet)options.bootstrapClasspath files(newFileList.toArray())}
}
//调整jar包的顺序
preBuild {doLast {def imlFile file(project.name .iml)println(Change project.name .iml order)try {def parsedXml (new XmlParser()).parse(imlFile)def jdkNode parsedXml.component[1].orderEntry.find { it.type jdk }parsedXml.component[1].remove(jdkNode)def sdkString Android API android.compileSdkVersion.substring(android-.length()) Platformnew groovy.util.Node(parsedXml.component[1], orderEntry, [type: jdk, jdkName: sdkString, jdkType: Android SDK])groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))} catch (FileNotFoundException e) {}}2.App层的实现App层实现通过反射来操作实现思路其实和系统层实现是一样的逻辑首先需要先获取EthernetManager对象接着创建StaticIpConfiguration对象再根据StaticIpConfiguration对象反射创建IpConfiguration对象和系统层对比App层是使用反射来获取对象而已逻辑是一样的以下是代码实现
class EthernetUtil Inject constructor(ApplicationContext private val context: Context,private val ipConfigRepository: IPConfigRepository,private val guideRepository: GuideRepository
) {/*** 设置以太网静态IP地址** param address ip地址* param mask 子网掩码* param gate 网关* param dns dns*/fun setEthernetStaticIp(address: String,mask: String,gate: String,dns: String,dns2: String): Boolean {d(TAG, setEthernetStaticIp -- ip: $address, mask: $mask, gate $gate, dns: $dns, dns2: $dns2)return try {val ethernetManagerCls Class.forName(android.net.EthernetManager)//获取EthernetManager实例SuppressLint(WrongConstant) val ethManager getApp().getSystemService(ethernet)val ipInfo ipConfigRepository.getDefaultIp()val primaryDNS if (dns.isNullOrEmpty() || dns.isNullOrBlank()) {ipInfo.IPAddress?.PrimaryDNS?.ipAddress} else {dns}val secondaryDNS if (dns2.isNullOrEmpty() || dns2.isNullOrBlank()) {ipInfo.IPAddress?.SecondaryDNS?.ipAddress} else {dns2}//创建StaticIpConfigurationval staticIpConfiguration newStaticIpConfiguration(address, gate, mask, primaryDNS ?: , secondaryDNS ?: )//创建IpConfigurationval ipConfiguration newIpConfiguration(staticIpConfiguration)//获取EthernetManager的setConfiguration()val setConfigurationMethod ethernetManagerCls.getDeclaredMethod(setConfiguration,*arrayOf(NET_ETH_0.javaClass, ipConfiguration.javaClass))//保存静态ip设置saveIpSettings(getApp(), address, mask, gate, dns, dns2)//保存和硬终端一致dns为空也配置//设置静态IPsetConfigurationMethod.invoke(ethManager, NET_ETH_0, ipConfiguration)guideRepository.guideStatus truetrue} catch (e: Exception) {e.printStackTrace()false}}/**** 创建StaticIpConfiguration对象*/Throws(Exception::class)private fun newStaticIpConfiguration(address: String,gate: String,mask: String,dns: String,dns2: String): Any {val staticIpConfigurationCls Class.forName(android.net.StaticIpConfiguration)//实例化StaticIpConfigurationval staticIpConfiguration staticIpConfigurationCls.newInstance()val ipAddress staticIpConfigurationCls.getField(ipAddress)val gateway staticIpConfigurationCls.getField(gateway)val domains staticIpConfigurationCls.getField(domains)val dnsServers staticIpConfigurationCls.getField(dnsServers)//设置ipAddressipAddress[staticIpConfiguration] newLinkAddress(address, mask)//设置网关gateway[staticIpConfiguration] InetAddress.getByName(gate)//设置掩码domains[staticIpConfiguration] mask//设置dns1val dnsList dnsServers[staticIpConfiguration] as ArrayListInetAddressdnsList.add(InetAddress.getByName(dns))//设置dns2dnsList.add(InetAddress.getByName(dns2))return staticIpConfiguration}/*** 获取LinkAddress实例*/Throws(Exception::class)private fun newLinkAddress(address: String, mask: String): Any? {val linkAddressCls Class.forName(android.net.LinkAddress)val linkAddressConstructor linkAddressCls.getDeclaredConstructor(InetAddress::class.java,Int::class.java)d(TAG, 子网掩码参数ip: ${InetAddress.getByName(address)} ${maskStr2InetMask(mask)})return linkAddressConstructor.newInstance(InetAddress.getByName(address),maskStr2InetMask(mask))}/*** 获取IpConfiguration实例*/Throws(Exception::class)private fun newIpConfiguration(staticIpConfiguration: Any): Any {val ipConfigurationCls Class.forName(android.net.IpConfiguration)val ipConfiguration ipConfigurationCls.newInstance()//设置StaticIpConfigurationval staticIpConfigurationField ipConfigurationCls.getField(staticIpConfiguration)staticIpConfigurationField[ipConfiguration] staticIpConfiguration//获取ipAssignment、proxySettings的枚举值val ipConfigurationEnum getIpConfigurationEnum(ipConfigurationCls)//设置ipAssignmentval ipAssignment ipConfigurationCls.getField(ipAssignment)ipAssignment[ipConfiguration] ipConfigurationEnum[IpAssignment.STATIC]//设置proxySettingsval proxySettings ipConfigurationCls.getField(proxySettings)proxySettings[ipConfiguration] ipConfigurationEnum[ProxySettings.STATIC]return ipConfiguration}/*** 获取IpConfiguration的枚举值*/private fun getIpConfigurationEnum(ipConfigurationCls: Class*): MapString, Any {val enumMap: MutableMapString, Any HashMap()val enumClass ipConfigurationCls.declaredClassesfor (enumC in enumClass) {val enumConstants enumC.enumConstants ?: continuefor (enu in enumConstants) {enumMap[enumC.simpleName . enu.toString()] enu}}return enumMap}/*** 保存静态ip设置*/private fun saveIpSettings(context: Context,address: String,mask: String,gate: String,dns: String,dns2: String) {val contentResolver context.contentResolverSettings.Global.putString(contentResolver, ethernet_static_ip, address)Settings.Global.putString(contentResolver, ethernet_static_mask, mask)Settings.Global.putString(contentResolver, ethernet_static_gateway, gate)Settings.Global.putString(contentResolver, ethernet_static_dns1, dns)Settings.Global.putString(contentResolver, ethernet_static_dns2, dns2)}/*** 获取子网掩码长度*/private fun maskStr2InetMask(maskStr: String): Int {var sb: StringBuffervar str: Stringvar inetmask 0var count 0/**** check mask format*/val pattern: Pattern Pattern.compile((^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$)if (pattern.matcher(maskStr).matches() false) {d(TAG, subMask is error)return 0}val ipSegment maskStr.split(\\..toRegex()).toTypedArray()for (n in ipSegment.indices) {sb StringBuffer(Integer.toBinaryString(ipSegment[n].toInt()))str sb.reverse().toString()count 0var i 0while (i str.length) {i str.indexOf(1, i)if (i -1) breakcounti}inetmask count}return inetmask}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/88370.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!