How can you set the http proxy programmatically?

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;


If you are limiting the use of proxies to your own application you can use the Proxy and ProxySelector API.


It's not possible to do this as a 3rd-party app. You get this message:

12-07 12:39:37.736: W/PackageManager(85): Not granting permission android.permission.WRITE_SECURE_SETTINGS to package com.mgranja.xxxxxxx (protectionLevel=3 flags=0xbe46)

Only apps that are signed with the same key as system apps can get this permission (i.e.: if you cook your own rom, you could add that funcionality)

More info about permission levels on this question, specially adamk's answer.

Why are these permissions being refused?

Tags:

Android