Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用WifiManager更改Wifi配置_Android_Connection_Wifi_Wifimanager - Fatal编程技术网

Android 使用WifiManager更改Wifi配置

Android 使用WifiManager更改Wifi配置,android,connection,wifi,wifimanager,Android,Connection,Wifi,Wifimanager,我正在使用WifiManager更改wifi设置,但当我以编程方式更改设置时,似乎什么都没有发生。 我想更改dns以阻止应用程序连接到internet。当我手动更改wifi配置时,一切正常,但当我使用我的应用程序更改它时,dns似乎没有更改,但在wifi高级选项中,它已更改但不工作 WifiConfiguration wifiConf = null; WifiManager wifiManager ; wifiManager = (WifiManager)getSystemService(Con

我正在使用
WifiManager
更改wifi设置,但当我以编程方式更改设置时,似乎什么都没有发生。 我想更改dns以阻止应用程序连接到internet。当我手动更改wifi配置时,一切正常,但当我使用我的应用程序更改它时,dns似乎没有更改,但在wifi高级选项中,它已更改但不工作

WifiConfiguration wifiConf = null;
WifiManager wifiManager ;
wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiInfo connectionInfo = wifiManager.getConnectionInfo();
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();   
for (WifiConfiguration conf : configuredNetworks){
    if (conf.networkId == connectionInfo.getNetworkId()){
        wifiConf = conf;
        break;              
                    }
                }

wifiManager对操作顺序非常敏感。你可以试试这道菜:1。创建或更新您的wifi配置2。UpdateNet()如果正在更新而不是创建3。saveConfiguration()3。addNetwork()TanQ,请给我一个示例代码
  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);
            }

            public static void setDNS(InetAddress dns,InetAddress dns2, WifiConfiguration wifiConf)
            throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{
                Object linkProperties = getField(wifiConf, "linkProperties");
                if(linkProperties == null)return;

                ArrayList<InetAddress> mDnses = (ArrayList<InetAddress>)getDeclaredField(linkProperties, "mDnses");
                mDnses.clear(); //or add a new dns address , here I just want to replace DNS1
                mDnses.add(dns); 
                mDnses.add(dns2);
            }

            public static Object getField(Object obj, String name)
            throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
                Field f = obj.getClass().getField(name);
                Object out = f.get(obj);
                return out;
            }

            public static Object getDeclaredField(Object obj, String name)
            throws SecurityException, NoSuchFieldException,
            IllegalArgumentException, IllegalAccessException {
                Field f = obj.getClass().getDeclaredField(name);
                f.setAccessible(true);
                Object out = f.get(obj);
                return out;
            }  

            public static void setEnumField(Object obj, String value, String name)
            throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{
                Field f = obj.getClass().getField(name);
                f.set(obj, Enum.valueOf((Class<Enum>) f.getType(), value));
            }
                 try{
                   setIpAssignment("STATIC", wifiConf); //or "DHCP" for dynamic setting
                  // setIpAddress(InetAddress.getByName("192.168.0.100"), 24, wifiConf);
                   WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                   int ip = wifiInfo.getIpAddress();
                   byte[] ipByteArray = BigInteger.valueOf(ip).toByteArray();

                 String ipStr = 
                          String.format("%d.%d.%d.%d",
                                 (ip & 0xff),   
                                 (ip >> 8 & 0xff),             
                                 (ip >> 16 & 0xff),    
                                 (ip >> 24 & 0xff));

                   String ipAddressString;
                   try {
                      ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
                   } catch (UnknownHostException ex) {

                   ipAddressString = null;
                   }
                   setIpAddress(InetAddress.getByName(ipStr), 24, wifiConf);
                   setGateway(InetAddress.getByName(ipStr), wifiConf);
                   setDNS(InetAddress.getByName("192.168.4.93"),InetAddress.getByName("192.168.4.94"), wifiConf);
                   wifiManager.saveConfiguration();
                   wifiManager.updateNetwork(wifiConf); //apply the setting
                   wifiManager.saveConfiguration(); //Save it
               }catch(Exception e){
                   e.printStackTrace();
               }