如何编程设置HTTP代理?HTTP

2023-09-13 01:51:20 作者:你撒謊了我裝做不知。

我在找一个纲领性的方式来建立HTTP代理服务器设置为Android手机。我已经使用android.provider.Settings.System.putString()来设置System.HTTP_PROXY尝试过,但我调用失败(我使用的是目前2.2仿真图像)。我的code是这样的:

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

我也加入到我的Andr​​oid清单:

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.

我熟悉这太线程,但该技术存在需要手动ADB命令,这需要SDK工具和(可能的)有根电话。

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.

有没有办法做到这一点?理想情况下,我想离开设置一个HTTP代理,将同时用于数据和WiFi连接。

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.

推荐答案

这是不可能做到这一点作为一个第三方的应用程序。你得到这个消息:

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)

只有那些具有相同的密钥系统的应用程序可以得到这样的许可签名的软件(例如:如果你煮自己的ROM,你可以添加的funcionality)

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)

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

More info about permission levels on this question, specially adamk's answer.

为什么这些权限被拒绝?