【问题标题】:How can you set the http proxy programmatically?如何以编程方式设置 http 代理?
【发布时间】:2010-09-02 17:35:31
【问题描述】:

我正在寻找一种编程方式来为安卓手机设置 http 代理设置。我尝试使用 android.provider.Settings.System.putString() 设置 System.HTTP_PROXY,但我的调用失败(我目前使用的是 2.2 模拟器图像)。我的代码如下:

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

..虽然从文档中并不清楚需要哪些权限(如果有的话)。

我熟悉this SO thread,但那里的技术需要手动 adb 命令,这需要 SDK 工具和(可能)有根电话。

有没有办法做到这一点?理想情况下,我想设置一个用于数据和 wifi 连接的 http 代理。

【问题讨论】:

    标签: android


    【解决方案1】:

    作为第 3 方应用程序无法执行此操作。您会收到以下消息:

    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,则可以添加该功能)

    有关此问题的权限级别的更多信息,特别是 adamk 的答案。

    Why are these permissions being refused?

    【讨论】:

    • 它可能会在旧设备上,我不确定 SElinux 强制规则...
    【解决方案2】:

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

    【讨论】:

    • 感谢您提供的信息 - 不幸的是,我正在尝试在设备上设置代理(例如,所有浏览器都会使用它..)。
    【解决方案3】:

    设置代理检查迈克的回答; 以下是获取代理详细信息的代码 sn-p

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

    如果检测到异常或未检测到代理,它将返回空;

    【讨论】:

      【解决方案4】:

      您可以为您的应用虚拟机设置代理,但出于安全原因,第三方应用可能没有设置设备代理的功能。

      【讨论】:

        【解决方案5】:

        我发现了一些 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” />
        

        到您的清单。

        【讨论】:

          【解决方案6】:

          您是否尝试过call the com.android.settings.ProxySelector 活动并让用户进入代理?它是全局存储的,但似乎不支持标准的 Proxy 和 ProxySelector API(对于这个问题,已经有另一个问题:How users/developers can set the Android's proxy configuration for versions 2.x?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-09-22
            • 1970-01-01
            • 1970-01-01
            • 2016-07-19
            • 2011-07-28
            相关资源
            最近更新 更多