Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you set the http proxy programmatically?

Tags:

android

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_PROXY, but my call fails (I'm using a 2.2 emulator image at the moment). My code looks like:

if (System.putString(getContentResolver(), System.HTTP_PROXY, "10.10.2.1:8080")) {
    tv.append("put for HTTP_PROXY succeeded.\n");
}
else {
    tv.append("put for HTTP_PROXY failed.\n");
}

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" />

..although it's not clear from the docs which permission, if any, is required.

I'm familiar with this SO thread, but the technique there requires manual adb commands, which require the SDK tools and (possibly) a rooted phone.

Is there a way to accomplish this? Ideally, I'd like away to set an http proxy that will be used for both data and wifi connections.

like image 272
Mike Ellery Avatar asked Sep 02 '10 17:09

Mike Ellery


People also ask

What should my HTTP proxy be set on?

One of the most crucial settings when setting up an HTTP proxy is the ports. Incorrect port settings will prevent you from establishing any connection. Typical HTTP ports are 80, 8080, and 465 if you want to use HTTPS. Your particular port numbers may be different and highly dependent on your ISP, firewall, and router.


2 Answers

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?

like image 84
M Granja Avatar answered Oct 26 '22 11:10

M Granja


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

like image 31
Jcs Avatar answered Oct 26 '22 12:10

Jcs