如何以编程方式设置http代理? [英] How can you set the http proxy programmatically?

查看:116
本文介绍了如何以编程方式设置http代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我还添加了我的android清单:

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.

我熟悉此SO线程,但是该技术需要手动的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,则可以添加该功能)

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.

为什么拒绝这些权限?

这篇关于如何以编程方式设置http代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆