猿问

如何在Android 3.x或4.x上以编程方式配置静态IP地址,网络掩码,网关

我已经检查了Stack Overflow问题API,以在Android应用程序中配置静态IP地址。


它可以在Android 2.3之前运行。但是,在更高的API级别上没有运气。例如,我把设置


android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_USE_STATIC_IP, "1");        

android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_IP, "192.168.0.100");

android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");

android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_DNS1, "192.168.0.254");

android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.254");

但是我回过头来检查:


Setting --> Wi-Fi --> Long Press Access Point SSID --> Modify Network --> check Show advanced options

该IP Settings字段仍然说明,DHCP但没有Static。


我确实可以android.provider.Settings.System.getString()用来找回设定的东西。证明该设置保存在某个地方,但是系统只是忽略它。


系统使用除android.provider.Settings.SystemAndroid 3.x和4.x上的设置以外的其他设置,因为该设置是根据访问点SSID设置的。是否可以像在Android 2.3上一样修改一个SSID上的设置?


米琪卡哇伊
浏览 939回答 3
3回答

慕容3067478

感谢您的解决方案对我在Android M 6.0.1上运行的My Nexus设备的运行正常。我已经更换了         // apply the configuration change        boolean result = wm.updateNetwork(wifiConf) != -1; //apply the setting        if(result) result = wm.saveConfiguration(); //Save it        if(result) wm.reassociate(); // reconnect with the new static IP与以下int netId = manager.updateNetwork(wifiConf);boolean result =  netId!= -1; //apply the settingif(result){    boolean isDisconnected =  manager.disconnect();    boolean configSaved = manager.saveConfiguration(); //Save it    boolean isEnabled = manager.enableNetwork(wifiConf.networkId, true);    // reconnect with the new static IP    boolean isReconnected = manager.reconnect();                        }

小怪兽爱吃肉

对于Android 5.0及更高版本的WIP解决方案。由于某种原因,它尚不起作用。欢迎发表评论。void changeWifiConfiguration(boolean dhcp, String ip, int prefix, String dns1, String gateway) {&nbsp; &nbsp; WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);&nbsp; &nbsp; if(!wm.isWifiEnabled()) {&nbsp; &nbsp; &nbsp; &nbsp; // wifi is disabled&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; // get the current wifi configuration&nbsp; &nbsp; WifiConfiguration wifiConf = null;&nbsp; &nbsp; WifiInfo connectionInfo = wm.getConnectionInfo();&nbsp; &nbsp; List<WifiConfiguration> configuredNetworks = wm.getConfiguredNetworks();&nbsp; &nbsp;&nbsp; &nbsp; if(configuredNetworks != null) {&nbsp; &nbsp; &nbsp; &nbsp; for (WifiConfiguration conf : configuredNetworks){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (conf.networkId == connectionInfo.getNetworkId()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wifiConf = conf;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if(wifiConf == null) {&nbsp; &nbsp; &nbsp; &nbsp; // wifi is not connected&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; Class<?> ipAssignment = wifiConf.getClass().getMethod("getIpAssignment").invoke(wifiConf).getClass();&nbsp; &nbsp; &nbsp; &nbsp; Object staticConf = wifiConf.getClass().getMethod("getStaticIpConfiguration").invoke(wifiConf);&nbsp; &nbsp; &nbsp; &nbsp; if(dhcp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wifiConf.getClass().getMethod("setIpAssignment", ipAssignment).invoke(wifiConf, Enum.valueOf((Class<Enum>) ipAssignment, "DHCP"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(staticConf != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; staticConf.getClass().getMethod("clear").invoke(staticConf);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wifiConf.getClass().getMethod("setIpAssignment", ipAssignment).invoke(wifiConf, Enum.valueOf((Class<Enum>) ipAssignment, "STATIC"));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(staticConf == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<?> staticConfigClass = Class.forName("android.net.StaticIpConfiguration");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; staticConf = staticConfigClass.newInstance();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // STATIC IP AND MASK PREFIX&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Constructor<?> laConstructor = LinkAddress.class.getConstructor(InetAddress.class, int.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LinkAddress linkAddress = (LinkAddress) laConstructor.newInstance(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InetAddress.getByName(ip),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prefix);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; staticConf.getClass().getField("ipAddress").set(staticConf, linkAddress);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // GATEWAY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; staticConf.getClass().getField("gateway").set(staticConf, InetAddress.getByName(gateway));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // DNS&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<InetAddress> dnsServers = (List<InetAddress>) staticConf.getClass().getField("dnsServers").get(staticConf);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dnsServers.clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dnsServers.add(InetAddress.getByName(dns1));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dnsServers.add(InetAddress.getByName("8.8.8.8")); // Google DNS as DNS2 for safety&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // apply the new static configuration&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wifiConf.getClass().getMethod("setStaticIpConfiguration", staticConf.getClass()).invoke(wifiConf, staticConf);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // apply the configuration change&nbsp; &nbsp; &nbsp; &nbsp; boolean result = wm.updateNetwork(wifiConf) != -1; //apply the setting&nbsp; &nbsp; &nbsp; &nbsp; if(result) result = wm.saveConfiguration(); //Save it&nbsp; &nbsp; &nbsp; &nbsp; if(result) wm.reassociate(); // reconnect with the new static IP&nbsp; &nbsp; } catch(Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }}
随时随地看视频慕课网APP
我要回答