改变无线先进的从code选项,但它不会工作? [英] changed wifi advanced option from code but it won't work?

查看:153
本文介绍了改变无线先进的从code选项,但它不会工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个code键更改无线IP设置(IP地址和网关)

您可以看到完整的code在这里:<一href="http://stackoverflow.com/questions/25141229/change-internet-proxy-wifi-3g-gprs-in-android-2-2-an-upper-from-$c$c">full类

  WifiConfiguration wifiConf = NULL;
    WifiManager经理=(WifiManager)getSystemService(MainActivity.WIFI_SERVICE);
        WifiInfo connectionInfo = manager.getConnectionInfo();
    名单&LT; WifiConfiguration&GT; configuredNetworks =经理
            .getConfiguredNetworks();

    wifiConf = GetCurrentWifiConfiguration(经理);

setIpAssignment(静态,wifiConf);
setIpAddress(InetAddress.getByName(192.2.2.2),24,wifiConf);
setGateway(InetAddress.getByName(192.2.2.22),wifiConf);
manager.updateNetwork(wifiConf);
manager.saveConfiguration();
 

功能:

 公共静态无效setIpAssignment(字符串赋值,WifiConfiguration wifiConf)
        抛出SecurityException异常,抛出:IllegalArgumentException,NoSuchFieldException,IllegalAccessException {
    setEnumField(wifiConf,分配,ipAssignment);
}


公共静态无效setIpAddress(InetAddress类地址,诠释prefixLength,WifiConfiguration wifiConf)
        抛出SecurityException异常,抛出:IllegalArgumentException,NoSuchFieldException,IllegalAccessException,
        NoSuchMethodException,ClassNotFoundException异常,InstantiationException,的InvocationTargetException {
    对象linkProperties = getfield命令(wifiConflinkProperties);
    如果(linkProperties == NULL)回报;
    类laClass =的Class.forName(android.net.LinkAddress);
    构造laConstructor = laClass.getConstructor(新类[] {InetAddress.class,int.class});
    对象linkAddress上= laConstructor.newInstance(地址,prefixLength);

    ArrayList的mLinkAddresses =(ArrayList中)getDeclaredField(linkProperties,mLinkAddresses);
    mLinkAddresses.clear();
    mLinkAddresses.add(linkAddress上);
}


公共静态无效setGateway(InetAddress类网关,WifiConfiguration wifiConf)
        抛出SecurityException异常,抛出:IllegalArgumentException,NoSuchFieldException,IllegalAccessException,
        ClassNotFoundException的,NoSuchMethodException,InstantiationException,的InvocationTargetException {
    对象linkProperties = getfield命令(wifiConflinkProperties);
    如果(linkProperties == NULL)回报;
    类routeInfoClass =的Class.forName(android.net.RouteInfo);
    构造routeInfoConstructor = routeInfoClass.getConstructor(新等级[] {InetAddress.class});
    对象routeInfo = routeInfoConstructor.newInstance(网关);

    ArrayList的mRoutes =(ArrayList中)getDeclaredField(linkProperties,mRoutes);
    mRoutes.clear();
    mRoutes.add(routeInfo);
}
 

问题是在这里:

这些codeS成功改变IP地址和网关,当你从你的android设备设置检查WiFi连接先进的选项,但实际上设置未设置!

我是如何理解这一点:

首先,我手动设置一个假的IP地址和网关,修改网络 - >高级选项。然后尝试打开一个网站,Chrome浏览器,没有互联网连接和它的工作。然后,我设置了相同的IP地址和网关从code连接的SSID,但它没有工作,仍然有互联网连接。

我使用code相同的设置和手动,但它不是从code的工作。在这里我做错了?

解决方案

  mWifiManager.updateNetwork(selectWIfiConfi);
mWifiManager.saveConfiguration();
mWifiManager.setWifiEnabled(假);

mWifiManager.setWifiEnabled(真正的);
 

我重新打开无线网络,并将其作品phone`s 4.2.2版本

I used this code to change wifi Ip setting (Ip Address and gateway)

you can see the full code here : full class

WifiConfiguration wifiConf = null;
    WifiManager manager = (WifiManager) getSystemService(MainActivity.WIFI_SERVICE);
        WifiInfo connectionInfo = manager.getConnectionInfo();
    List<WifiConfiguration> configuredNetworks = manager
            .getConfiguredNetworks();

    wifiConf = GetCurrentWifiConfiguration(manager);

setIpAssignment("STATIC", wifiConf); 
setIpAddress(InetAddress.getByName("192.2.2.2"), 24, wifiConf);
setGateway(InetAddress.getByName("192.2.2.22"), wifiConf);
manager.updateNetwork(wifiConf); 
manager.saveConfiguration(); 

functions:

 public static void setIpAssignment(String assign , WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{
    setEnumField(wifiConf, assign, "ipAssignment");
}


public static void setIpAddress(InetAddress addr, int prefixLength, WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException,
        NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException{
    Object linkProperties = getField(wifiConf, "linkProperties");
    if(linkProperties == null)return;
    Class laClass = Class.forName("android.net.LinkAddress");
    Constructor laConstructor = laClass.getConstructor(new Class[]{InetAddress.class, int.class});
    Object linkAddress = laConstructor.newInstance(addr, prefixLength);

    ArrayList mLinkAddresses = (ArrayList)getDeclaredField(linkProperties, "mLinkAddresses");
    mLinkAddresses.clear();
    mLinkAddresses.add(linkAddress);
}


public static void setGateway(InetAddress gateway, WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException,
        ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException {
    Object linkProperties = getField(wifiConf, "linkProperties");
    if(linkProperties == null)return;
    Class routeInfoClass = Class.forName("android.net.RouteInfo");
    Constructor routeInfoConstructor = routeInfoClass.getConstructor(new Class[]{InetAddress.class});
    Object routeInfo = routeInfoConstructor.newInstance(gateway);

    ArrayList mRoutes = (ArrayList)getDeclaredField(linkProperties, "mRoutes");
    mRoutes.clear();
    mRoutes.add(routeInfo);
}

problem is here :

these codes successfully change the ip address and gateway when you check wifi connection advanced option from setting in your android device, but the setting actually isn't set!!!

how I understand this :

first I set a fake Ip address and gateway manually, modify network-> advanced option. and then try to open a website with chrome, no internet access and it worked. then I set the same Ip address and gateway for connected ssid from code but It's not worked and still have internet access.

I used same settings from code and manually but its not worked from code. where I did wrong ?

解决方案

mWifiManager.updateNetwork(selectWIfiConfi);
mWifiManager.saveConfiguration();
mWifiManager.setWifiEnabled(false);

mWifiManager.setWifiEnabled(true);

I reopen wifi and it works phone`s version 4.2.2

这篇关于改变无线先进的从code选项,但它不会工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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