[Android] How to set proxy for android browser

There are many reasons to make browser serf pages through proxy server:

  • someone wanna catch http requests/responses
  • someone wanna hide his IP
  • so on

What to do if you want set proxy for android browser? there some ways:

  • add record to database: /data/data/com.android.providers.settings/databases/settings.db
  1. pull database to pc add record (using for example sdk tool sqlite3) and replace existing db
  2. make changes in database directly on device

but as for me there exist simplier way, do it by your Java application using Settings provider:

Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "proxy_ip:proxy_port");

where proxy_ip/proxy_port = IP/port of proxy that you going to use.

there left one problem, it will not work if we will not add one string to manifest file, here it is:

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

Thats all, now it works, here is code:

package com.BrowserSettings;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.provider.Settings;

public class BrowserSettingsUI extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
try
{
Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "127.0.0.1:100");//enable proxy
}catch (Exception ex){
}
}
});

final Button button2 = (Button) findViewById(R.id.Button02);
button2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {

try
{
Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, "");//disable proxy
}catch (Exception ex){
}
}
});

}
}

manifest file:

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.BrowserSettings”
android:versionCode=”1″
android:versionName=”1.0.0″>
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.BrowserSettingsUI”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
<uses-permission android:name=”android.permission.WRITE_SETTINGS” />
</manifest>

27 Responses to “[Android] How to set proxy for android browser”

  1. 1
    Billy Says:

    did it work for G1 or just the emulator?

  2. 2
    admin Says:

    i`ve tested only on Emulator, but i think it should work on real device too

  3. 3
    riddick Says:

    I have a HTC Magic, how can i compile this code for try it?

  4. 4
    admin Says:

    you need:
    1. andriod sdk
    2. eclipse
    3. android plug-in for eclipse

    i think if you install and configure all from required list it wont be a problem to build application that can set proxy

  5. 5
    Nathan Says:

    Thanks for the great tip… I’m working on a port of Tor (http://torproject.org) and figuring out how to programatically turn the web proxy on and off was one of the last pieces I needed.

  6. 6
    links for 2009-09-08 | Nathan and his Open Ideals Says:

    […] Android Set up Proxy | Alex Mogurenko`s Blog There are many reasons to make browser serf pages through proxy server: […]

  7. 7
    Ji Xiao Says:

    How do we add exception list and username passcode for proxy.

  8. 8
    Ji Xiao Says:

    As you know, we need to add an exception list in which the web page would NOT be visited through proxy server?

  9. 9
    Joe Says:

    Now it seems to need Settings.Secure and that to have this permission (WRITE_SECURE_SETTINGS) an app needs to be signed with the platform certificate- I’ve not worked out how to do that…

  10. 10
    voss Says:

    In general, well done. But, the problem is that this funktion (Settings.System) has been moved to Settings.Secure, which will need other Use Permission Settings (WRITE_SECURE_SETTINGS) which will not be granted to normal applications.

    So this way of setting proxy stuff has been closed by Google —

    From my point of view this is somewhat of paranoic !

  11. 11
    Ali Says:

    I am behind a corporate proxy so not only I need to provide Proxy HOST and PORT but also Username and Password. Any idea how I can do that?

    Thanks

  12. 12
    bartoz Says:

    I tried to build the app as you suggest, but if I lanch it I get a force close..
    Could you please revise the code (or even build a usable apk)?
    I’d really appreciate!

  13. 13
    Murali Says:

    I am also behind a corporate proxy. I should use my username and password.
    How should I specify the user name and password?

  14. 14
    funktionierendes Proxy APP - Android-Hilfe.de Says:

    […] abnimmt, kenne ich auch nicht. Let’s talk about Google Android: Set proxy for android web browser Android Set up Proxy | Alex Mogurenko`s Blog Evtl. ProxySurf v1.1 Application for Android | Communication ? Sieht aber eher nicht so gut aus. […]

  15. 15
    wifi polimi - Pagina 3 - Forum Android Italiano Says:

    […] Ci sto sbattendo la testa da due giorni e pare che questo sia l’unica via percorribile…ma mi sembra un gran […]

  16. 16
    Manuel Says:

    Hi, I compiled the code but it doesn’t work in my mobile. I have an HTC Tattoo with Android 1.6

    If anyone wants to try, I uploaded the application .apk, you can download it here: http://www.megaupload.com/?d=NLNYQ4S3 . It’s just Alex’s code with an additional text line saying if the proxy is on or off. It doesn’t work for me, but maybe you can give it a try.

  17. 17
    Manuel Says:

    With the message before I meant that the code works fine and doesn’t produce any errors, but that I’m still unable to use the internet under a proxy.

  18. 18
    Vodafone live! Internet Flat mit Android Tunneln? Proxycap f?r Android? - HTC Desire - Android Forum Says:

    […] routen? Gib mal bei google "android" und "proxy" ein, da kommt z.B. diese Seite, wo man nach etwas Handarbeit einen Proxy f?r den Browser hat. Oder du suchst dir ne App im […]

  19. 19
    Robin van Leeuwen Says:

    Ok, i implemented it in the Android SDK 2.1 and it didn’t work, so i build in a check to see if the value was set correctly. Am i doing something wrong? I built in the check:
    ————-
    TextView statusbar = (TextView) findViewById(R.id.TextView01);
    String proxy = Settings.System.getString(getContentResolver(),Settings.System.HTTP_PROXY);
    statusbar.setText(”The proxy is now: “+proxy);
    ————-
    The value is consistently ‘null’ at the beginning before doing anything, after setting (button 1) and after disabling (button 2). Am i doing the check wrong, or is the http_proxy indeed not set?

  20. 20
    AlexJ Says:

    username:password@ip.ip.ip.ip:port

  21. 21
    Greg Ruo Says:

    Using ECLIPSE I modified a “helloworld”-kind application, added the permission in the androidmanifest.xml () and included the Setting suggested after a text message. No compiler errors, transferred on device (HTC desire), but no results… still I cannot browse internet.

    Probably the “set” command is not really working, or it is inhibited by permission problem…?

    Greg Ruo

    PS: Below is the code I used

    ================================
    package com.BrowserSettings;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    import android.provider.Settings;

    public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setContentView(R.layout.main);
    TextView tv = new TextView(this);
    tv.setText(”! Should Set proxy OK !!”);
    setContentView(tv);
    Settings.System.putString(getContentResolver(), Settings.System.HTTP_PROXY, “xxx.xxx.xxx.xxx:8080″);
    }
    }
    =========================================-

  22. 22
    christian louboutin sale Says:

    Yes, it works at my Hero

  23. 23
    circle Says:

    According to API document, the Settings.System.HTTP_PROXY setting is deprecated:
    http://developer.android.com/reference/android/provider/Settings.System.html#HTTP_PROXY

    and the new one is under Settings.Secure:
    http://developer.android.com/reference/android/provider/Settings.Secure.html

    However, it wrote:
    “Secure system settings, containing system preferences that applications can read but are not allowed to write. These are for preferences that the user must explicitly modify through the system UI or specialized APIs for those values, not modified directly by applications. ”

    It it seems to be impossible to change like this now?

    Or like Joe at #9 saying, need some permission?

  24. 24
    links for 2010-08-31 | Says:

    […] Android Set up Proxy | Alex Mogurenko`s Blog (tags: android) […]

  25. 25
    guest Says:

    just install with a easy way…(note you need root access for that)

    http://forum.xda-developers.com/showthread.php?t=766569

  26. 26
    Fabio Says:

    An easer way: http://androidero.blogspot.com/2010/10/acessando-wifi-via-proxy.html

  27. 27
    GFW BLOG(??????): ?Android???????? Says:

    […] ?????????([Android] How to set proxy for android browser),???????????????。???????,????,?????。 […]

Leave a Reply