亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在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 關注
  • 983 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號