2010-09-02 42 views
11

我正在尋找一種編程方式來設置android手機的http代理設置。我試過使用android.provider.Settings.System.putString()來設置System.HTTP_PROXY,但是我的調用失敗了(我現在使用的是2.2模擬器鏡像)。我的代碼如下所示:如何以編程方式設置http代理?

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"); 
} 

我也加入到我的Android清單:

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

..although目前還不清楚從哪個許可,如果有的話,需要的文檔。

我對this SO thread很熟悉,但那裏的技術需要手動adb命令,它需要SDK工具和(可能)紮根電話。

有沒有辦法做到這一點?理想情況下,我想要設置將用於數據和WiFi連接的http代理。

回答

14

這是不可能做到這一點的第三方應用程序。你得到這個消息:

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) 

只有應用程序,使用相同的密鑰簽名的系統應用能得到這個許可(即:如果你煮自己的ROM,你可以添加的funcionality)

更多信息關於這個問題的權限級別,特別是adamk的回答。

Why are these permissions being refused?

+0

它可以在Rooted設備上工作嗎? – 2016-06-06 11:08:10

+1

它可能會在較舊的設備上,我不確定有關SElinux執行規則... – 2016-06-07 18:54:02

10

如果您將代理的使用限制在您自己的應用程序中,則可以使用ProxyProxySelector API。

+0

感謝您的信息 - 不幸的是我試圖設置代理通常在設備上(以便所有瀏覽器將使用它,例如..)。 – 2010-09-02 22:55:29

1

您可以爲應用程序虛擬機設置代理,但由於安全原因,第三方應用程序可能沒有設置設備代理的功能。

-1

我發現一些here,看起來像它可能工作

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){ 
       } 
      } 
     }); 
    } 
} 

您必須添加

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

到您的清單。

3

要設置代理檢查邁克的答案; 以下是代碼片段檢索代理細節

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; 
    } 

如果一些異常或無代理探測到它會返回空;

相關問題