如何配置一个静态IP地址,子网掩码,网关,DNS编程在Android 5.x的(棒棒堂)的Wi-Fi连接 [英] How to configure a static IP address, netmask, gateway, DNS programmatically on Android 5.x (Lollipop) for Wi-Fi connection

查看:1512
本文介绍了如何配置一个静态IP地址,子网掩码,网关,DNS编程在Android 5.x的(棒棒堂)的Wi-Fi连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何静态IP地址,子网掩码,网关,DNS通过程序在Android 5.x的配置了Wi-Fi连接?是否有一个开放的API(未找到)或隐藏的可用于本?你能不能也请大家举一些例子,如果可能的。

How can a static IP address, netmask, gateway, DNS be programmatically configured on Android 5.x for Wi-Fi connection? Is there an open API (didn't find it) or hidden can be used for this? Could you also please give some examples if possible.

我知道这是可以在Android 4.0+ ,但它并没有在Android 5.0的工作

I know it's possible on Android 4.0+ but it doesn't work on Android 5.0

推荐答案

目前还没有开放API,很遗憾。

There is still no open API, unfortunately.

对于Android 4.0的解决方案并不在LOLLIPOP工作,因为事情已经搬来搬去。特别是新的 IpConfiguration 类现在持有的<一个href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/net/StaticIpConfiguration.java"><$c$c>StaticIpConfiguration而所有这些领域。他们仍然可以通过使用反射(与所有引起该脆性)用这样的访问。

The solution for Android 4.0 doesn't work in LOLLIPOP because things have been moved around. In particular the new IpConfiguration class now holds the StaticIpConfiguration and all these fields. They can still be accessed by using reflection (with all the brittleness that entails) with something like this.

警告,这code的是Android 5.0只。你需要检查 Build.VERSION.SDK_INT 并据此采取行动。

Warning, this code is Android 5.0-only. You'll need to check Build.VERSION.SDK_INT and act accordingly.

@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();
}

通过下面的辅助方法来处理反射:

With the following helper methods to handle reflection:

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);
}

例如,你可以这样调用它:

For example, you can call it like this:

public void test(Context context)
{
    WifiManager manager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    WifiConfiguration wifiConf = ... /* Get Wifi configuration you want to update */

    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();
        }
    }
}

作为一个参考,你可能想看看的<一个href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/5.0.0_r1/com/android/settings/wifi/WifiConfigController.java"><$c$c>WifiConfigController在框架(尽管它使用这些类,而不是直接通过反射)类。

As a reference, you might want to take a look at the WifiConfigController class in the framework (though it uses these classes directly instead of via reflection).

这篇关于如何配置一个静态IP地址,子网掩码,网关,DNS编程在Android 5.x的(棒棒堂)的Wi-Fi连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆