为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

米琪卡哇伊 2019-09-21 11:37:27
我已经检查了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上的设置?
查看完整描述

3 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

感谢您的解决方案对我在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 setting

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

}


查看完整回答
反对 回复 2019-09-21
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

对于Android 5.0及更高版本的WIP解决方案。由于某种原因,它尚不起作用。欢迎发表评论。


void changeWifiConfiguration(boolean dhcp, String ip, int prefix, String dns1, String gateway) {

    WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);

    if(!wm.isWifiEnabled()) {

        // wifi is disabled

        return;

    }

    // get the current wifi configuration

    WifiConfiguration wifiConf = null;

    WifiInfo connectionInfo = wm.getConnectionInfo();

    List<WifiConfiguration> configuredNetworks = wm.getConfiguredNetworks();   

    if(configuredNetworks != null) {

        for (WifiConfiguration conf : configuredNetworks){

            if (conf.networkId == connectionInfo.getNetworkId()){

                wifiConf = conf;

                break;              

            }

        }

    }

    if(wifiConf == null) {

        // wifi is not connected

        return;

    }

    try {

        Class<?> ipAssignment = wifiConf.getClass().getMethod("getIpAssignment").invoke(wifiConf).getClass();

        Object staticConf = wifiConf.getClass().getMethod("getStaticIpConfiguration").invoke(wifiConf);

        if(dhcp) {

            wifiConf.getClass().getMethod("setIpAssignment", ipAssignment).invoke(wifiConf, Enum.valueOf((Class<Enum>) ipAssignment, "DHCP"));

            if(staticConf != null) {

                staticConf.getClass().getMethod("clear").invoke(staticConf);

            }

        } else {

            wifiConf.getClass().getMethod("setIpAssignment", ipAssignment).invoke(wifiConf, Enum.valueOf((Class<Enum>) ipAssignment, "STATIC"));

            if(staticConf == null) {

                Class<?> staticConfigClass = Class.forName("android.net.StaticIpConfiguration");

                staticConf = staticConfigClass.newInstance();

            }

            // STATIC IP AND MASK PREFIX

            Constructor<?> laConstructor = LinkAddress.class.getConstructor(InetAddress.class, int.class);

            LinkAddress linkAddress = (LinkAddress) laConstructor.newInstance(

                    InetAddress.getByName(ip), 

                    prefix);

            staticConf.getClass().getField("ipAddress").set(staticConf, linkAddress);

            // GATEWAY

            staticConf.getClass().getField("gateway").set(staticConf, InetAddress.getByName(gateway));

            // DNS

            List<InetAddress> dnsServers = (List<InetAddress>) staticConf.getClass().getField("dnsServers").get(staticConf);

            dnsServers.clear();

            dnsServers.add(InetAddress.getByName(dns1)); 

            dnsServers.add(InetAddress.getByName("8.8.8.8")); // Google DNS as DNS2 for safety

            // apply the new static configuration

            wifiConf.getClass().getMethod("setStaticIpConfiguration", staticConf.getClass()).invoke(wifiConf, staticConf);

        }

        // 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

    } catch(Exception e) {

        e.printStackTrace();

    }

}


查看完整回答
反对 回复 2019-09-21
  • 3 回答
  • 0 关注
  • 938 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信