How can you set the http proxy programmatically?

后端 未结 6 1932
暗喜
暗喜 2020-12-06 05:30

I\'m looking for a programmatic way to set-up http proxy settings for android handsets. I\'ve tried using android.provider.Settings.System.putString() to set System.HTTP_PR

6条回答
  •  半阙折子戏
    2020-12-06 06:16

    To set the proxy check Mike's answer; Following is code snippet to retrieve proxy details

    public static String getProxyDetails(Context context) {
            String proxyAddress = new String();
            try {
                if (IsPreIcs()) {
                    proxyAddress = android.net.Proxy.getHost(context);
                    if (proxyAddress == null || proxyAddress.equals("")) {
                        return proxyAddress;
                    }
                    proxyAddress += ":" + android.net.Proxy.getPort(context);
                } else {
                    proxyAddress = System.getProperty("http.proxyHost");
                    proxyAddress += ":" + System.getProperty("http.proxyPort");
                }
            } catch (Exception ex) {
                //ignore
            }
            return proxyAddress;
        }
    

    It'll return empty if some exception or no proxy detected;

提交回复
热议问题