[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”

Pages: « 3 2 [1] Show All

  1. 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 !

  2. 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…

  3. 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?

  4. 7
    Ji Xiao Says:

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

  5. 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: […]

  6. 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.

  7. 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

  8. 3
    riddick Says:

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

  9. 2
    admin Says:

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

  10. 1
    Billy Says:

    did it work for G1 or just the emulator?

Pages: « 3 2 [1] Show All

Leave a Reply