Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting WiFi proxy settings in Android

I am trying to read WIFI proxy settings

  • Proxy host
  • Proxy port
  • Proxy user (authentication)
  • Proxy password (authentication)

from devices in android versions 2.X.X – 4.X.X without any success.

Calling:

String proxy = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.HTTP_PROXY);

Always returns null.

I've also added to my android manifest:

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

still it returns null.

Also tried:

android.net.Proxy. getHost(Context ctx) – which is deprecated – returns the IP
android.net.Proxy. getPortt(Context ctx) – which is deprecated – returns always -1.

Java calls:

System.getProperty("http.proxyHost");
System.getProperty("http.proxyCall");

Also returns null.

Is there a working code which retrieves all these settings or at least partially from devices in all android versions?

like image 496
user1222022 Avatar asked May 30 '12 07:05

user1222022


People also ask

How do I Change my proxy settings on Android?

Open Android’s Settings app and tap “Wi-Fi” to view a list of Wi-Fi networks. Long-press the name of the Wi-Fi network you want to change the proxy settings for.

How do I enable Wi-Fi proxy on my Samsung device?

Samsung has slightly different Wi-Fi proxy settings on their Android devices. Open Settings and tap on Connections. Tap the first option on the screen, Wi-Fi. NOTE: The Wi-Fi option has to be enabled on your Samsung Android if you want to continue. You are shown a list of all the available Wi-Fi networks.

Can I use the same Android proxy settings for different Wi-Fi networks?

Even if you need to use the same Android proxy settings for different Wi-Fi networks, you must set them up one by one. To configure the Wi-Fi proxy settings, Android requires slightly different steps, depending on whether you’re setting the proxy server for an existing connection or a new one.

How do I connect to a proxy server on Android?

Alternatively, type the URL of the proxy server itself, and press Connect. If all your settings are correct, your Android smartphone or tablet is going to connect to the Wi-Fi network of your choice and use the proxy server that you have entered, each time it is connected to that network.


1 Answers

I found this project: Android Proxy Library Which provides backward compatible ways of querying Proxy settings as well as setting them for WebViews on older versions of Android.

    // Grab Proxy settings in a backwards compatible manner
    ProxyConfiguration proxyConfig = ProxySettings.getCurrentHttpProxyConfiguration( context );

    // Set Proxy for WebViews on older versions of Android
    ProxyUtils.setWebViewProxy( getActivity().getApplicationContext() );

However, there is something you need to understand about Proxy Settings set on a WiFi AP. Since WiFi specific Proxy Settings were not implemented in Android proper until 3.1, all pre-3.1 devices that expose that functionality are using some sort of custom hack. They don't work in any sort of standard way. So libraries like this won't be able to grab any proxy set from one of those hacks.

There is however a System Wide Proxy in pre-3.1 that this sort of library WILL grab. Of course Android saw fit not to provide any official way to modify this setting. But there are apps on the Play Store that will allow you to do it, this is the one I'm using: Proxy Settings and it works well, setting the System Proxy and allowing you to grab it either via this library, or even simpler methods like querying the JVM proxy settings.

I ended up not using the APL and instead went with a much simpler implementation:

    private static final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;

    ...

    String proxyAddress;
    int proxyPort;

    if( IS_ICS_OR_LATER )
    {
        proxyAddress = System.getProperty( "http.proxyHost" );

        String portStr = System.getProperty( "http.proxyPort" );
        proxyPort = Integer.parseInt( ( portStr != null ? portStr : "-1" ) );
    }
    else
    {
        proxyAddress = android.net.Proxy.getHost( context );
        proxyPort = android.net.Proxy.getPort( context );
    }
like image 54
Adam Avatar answered Oct 20 '22 19:10

Adam