栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Android 5.x(Lollipop)上以编程方式配置静态IP地址,网络掩码,网关和DNS以进行Wi-Fi连接

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Android 5.x(Lollipop)上以编程方式配置静态IP地址,网络掩码,网关和DNS以进行Wi-Fi连接

不幸的是,仍然没有开放的API。

Android
4.0的解决方案在LOLLIPOP中不起作用,因为事情已经转移了。特别是新

IpConfiguration
类现在包含
StaticIpConfiguration
和所有这些字段。仍然可以通过使用类似这样的反射(带有所有的脆性)来访问它们。

警告,此代码仅适用于Android 5.0。 您需要检查

Build.VERSION.SDK_INT
并采取相应措施。

@SuppressWarnings("unchecked")private static void setStaticIpConfiguration(WifiManager manager, WifiConfiguration config, InetAddress ipAddress, int prefixLength, InetAddress gateway, InetAddress[] dns) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException, InstantiationException{    // First set up IpAssignment to STATIC.    Object ipAssignment = getEnumValue("android.net.IpConfiguration$IpAssignment", "STATIC");    callMethod(config, "setIpAssignment", new String[] { "android.net.IpConfiguration$IpAssignment" }, new Object[] { ipAssignment });    // Then set properties in StaticIpConfiguration.    Object staticIpConfig = newInstance("android.net.StaticIpConfiguration");    Object linkAddress = newInstance("android.net.linkAddress", new Class<?>[] { InetAddress.class, int.class }, new Object[] { ipAddress, prefixLength });    setField(staticIpConfig, "ipAddress", linkAddress);    setField(staticIpConfig, "gateway", gateway);    getField(staticIpConfig, "dnsServers", ArrayList.class).clear();    for (int i = 0; i < dns.length; i++)        getField(staticIpConfig, "dnsServers", ArrayList.class).add(dns[i]);    callMethod(config, "setStaticIpConfiguration", new String[] { "android.net.StaticIpConfiguration" }, new Object[] { staticIpConfig });    manager.updateNetwork(config);    manager.saveConfiguration();}

使用以下辅助方法来处理反射:

private static Object newInstance(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{    return newInstance(className, new Class<?>[0], new Object[0]);}private static Object newInstance(String className, Class<?>[] parameterClasses, Object[] parameterValues) throws NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException{    Class<?> clz = Class.forName(className);    Constructor<?> constructor = clz.getConstructor(parameterClasses);    return constructor.newInstance(parameterValues);}@SuppressWarnings({ "unchecked", "rawtypes" })private static Object getEnumValue(String enumClassName, String enumValue) throws ClassNotFoundException{    Class<Enum> enumClz = (Class<Enum>)Class.forName(enumClassName);    return Enum.valueOf(enumClz, enumValue);}private static void setField(Object object, String fieldName, Object value) throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException{    Field field = object.getClass().getDeclaredField(fieldName);    field.set(object, value);}private static <T> T getField(Object object, String fieldName, Class<T> type) throws IllegalAccessException, IllegalArgumentException, NoSuchFieldException{    Field field = object.getClass().getDeclaredField(fieldName);    return type.cast(field.get(object));}private static void callMethod(Object object, String methodName, String[] parameterTypes, Object[] parameterValues) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException{    Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];    for (int i = 0; i < parameterTypes.length; i++)        parameterClasses[i] = Class.forName(parameterTypes[i]);    Method method = object.getClass().getDeclaredMethod(methodName, parameterClasses);    method.invoke(object, parameterValues);}

例如,您可以这样称呼它:

public void test(Context context){    WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);    WifiConfiguration wifiConf = ...     if (wifiConf != null)    {        try        { setStaticIpConfiguration(manager, wifiConf,     InetAddress.getByName("10.0.0.1"), 24,     InetAddress.getByName("10.0.0.2"),     new InetAddress[] { InetAddress.getByName("10.0.0.3"), InetAddress.getByName("10.0.0.4") });        }        catch (Exception e)        { e.printStackTrace();        }    }}

作为参考,您可能想看一下

WifiConfigController
框架中的类(尽管它直接使用这些类而不是通过反射来使用)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595281.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号