android WIFI 設定代理代碼 4.4.3——5.0

來源:互聯網
上載者:User

標籤:ipo   conf   pop   span   declare   lol   param   illegal   out   

記錄下android中設定代理代碼 或許有朋友能用的上

適用於4.4.3 在5.0上android.net.ProxyProperties 找不到 估計API被Google拿掉了 4.4.4還沒試估計API還在

private static String NOTPROXY = ""; //不走代理名單     private  static   List<String> list;    //type為1設定wifi設定   為0是清除代理    public static void setWifi(Context context, final String ip, final int port, final int type) throws SecurityException, IllegalArgumentException,            NoSuchFieldException, IllegalAccessException,            NoSuchMethodException, ClassNotFoundException,            InstantiationException, InvocationTargetException {        final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);        final String ssid = wifiManager.getConnectionInfo().getSSID();        List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();       changeNotProxy();        for (WifiConfiguration config : configuredNetworks) {            Log.d("txy", "current SSID is" + config.SSID);            if (config.SSID.toString().contains(Common.MYSSID)) {  //需要給哪個無線設定代理                Class proxyPropertiesClass = Class                        .forName("android.net.ProxyProperties");                Constructor c = proxyPropertiesClass.getConstructor(String.class,                        Integer.TYPE, String.class);                c.setAccessible(true);                //wifiIP 為代理的IP地址  如:192.168.0.111   wifiPort為代理連接埠 如888                setProxy(config, (type == 0 ? null : c.newInstance(ip, port, NOTPROXY)), type);                wifiManager.updateNetwork(config);                break;            } else {//                wifiManager.removeNetwork(config.networkId);            }        }        wifiManager.disconnect();        wifiManager.reconnect();    }    public static void setObjField(Object obj, Object value, String name)            throws SecurityException, NoSuchFieldException,            IllegalArgumentException, IllegalAccessException {        Field f = obj.getClass().getDeclaredField(name);        f.setAccessible(true);        f.set(obj, value);    }    //type為1設定wifi設定   為0是清除代理    public static WifiConfiguration setProxy(WifiConfiguration wifiConf, Object properties, int type)            throws SecurityException, IllegalArgumentException,            NoSuchFieldException, IllegalAccessException,            NoSuchMethodException, ClassNotFoundException,            InstantiationException, InvocationTargetException {        if (type == 0) setEnumField(wifiConf, "NONE", "proxySettings");        else setEnumField(wifiConf, "STATIC", "proxySettings");        Object linkProperties = getField(wifiConf, "linkProperties");        setObjField(linkProperties, properties, "mHttpProxy");        setObjField(wifiConf, linkProperties, "linkProperties");        return wifiConf;    }    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));    }    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;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

5.0

  private  static   List<String> list;//不走代理名單  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)    public static void set_proxy(Application context, String ip, String port, int type) throws Throwable    {        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);        WifiInfo wi = wifiManager.getConnectionInfo();        List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();        changeNotProxy();        for(WifiConfiguration config : configuredNetworks)        {            //需要給哪個無線設定代理            if(config.SSID.equals(wi.getSSID()))            {                Class proxySettings = Class.forName("android.net.IpConfiguration$ProxySettings");                Class[] setProxyParams = new Class[2];                setProxyParams[0] = proxySettings;                setProxyParams[1] = ProxyInfo.class;                Method setProxy = config.getClass().getDeclaredMethod("setProxy", setProxyParams);                setProxy.setAccessible(true);                if(type == 0)                {                    Object[] methodParams = new Object[2];                    methodParams[0] = Enum.valueOf(proxySettings, "NONE");                    methodParams[1] = null;                    setProxy.invoke(config, methodParams);                }                else                {                    ProxyInfo desiredProxy = ProxyInfo.buildDirectProxy(ip, Integer.parseInt(port),list);                    Object[] methodParams = new Object[2];                    methodParams[0] = Enum.valueOf(proxySettings, "STATIC");                    methodParams[1] = desiredProxy;                    setProxy.invoke(config, methodParams);                }                //save the settings                wifiManager.updateNetwork(config);                wifiManager.disconnect();                wifiManager.reconnect();                break;            }        }    }

android WIFI 設定代理代碼 4.4.3——5.0

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.