Android 应用程序如何决定是否要使用网络代理 [英] How Android application decide if they want to use network proxy or not

查看:28
本文介绍了Android 应用程序如何决定是否要使用网络代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在运行 Android 3.2 的摩托罗拉 Xoom 上使用 wifi 代理设置进行一些测试.因此,首先,与 2.x 版本相比,这是向前迈出的一大步.现在,如果您设置代理,大多数应用程序会自动获取它(在 2.x 中,只有内置浏览器使用它).所以我尝试了 yahoo 金融、bloomberg 等,他们都通过代理罚款.我不明白的是一些浏览器,如 firefox、Opera,不会通过代理.知道他们是怎么做到的.基本上在我的应用程序中,我如何决定是要使用代理还是尝试直接连接.根据我的测试,如果我们不做任何特殊的事情,默认是使用代理.那么我需要做什么才能让我的应用绕过 Firefox/Opera 等代理?

I have been running some testing with wifi proxy settings on a Motorola Xoom with Android 3.2. So first of all, it is a big step forward comparing to 2.x releases. now if you set proxy, most of the apps automatically get it (in 2.x, only builtin browser uses it). So I tried things like yahoo finance, bloomberg, etc. and they all going through proxy fine. What I don't get is some browsers like firefox, Opera, will not go through proxy. Any idea how they did that. Basically in my app, how can I decide if I want to use proxy or try to connect directly. Based on my testing, if we don't do anything special, the default is using proxy. So what do I need to do to allow my app bypass proxy like Firefox/Opera?

谢谢!

推荐答案

在 API 版本 >=11(Android 3.1 及更高版本)的设备上,答案在这里:

On devices with API version >=11 (Android 3.1 and greater) the answer is here:

Android 的代理混淆文档资源

您可以简单地从 ProxySelector 类调用 getDefault() 方法并获取 ProxySelector 的默认 Android 实现.

You can simply call the getDefault() method from ProxySelector class and get the default Android implementation of the ProxySelector.

ProxySelector defaultProxySelector = ProxySelector.getDefault();
Proxy proxy = null;
List<Proxy> proxyList = defaultProxySelector.select(uri);
if (proxyList.size() > 0)
{
  proxy = proxyList.get(0);
  Log.d(TAG, "Current Proxy Configuration: " + proxy.toString());
}

我认为某些 Android 应用程序(您说的是 Opera 和 Firefox)根本不执行此检查,而是实现了一些本地代理处理,而不关心系统的工作方式.

I think that some Android applications (you said Opera and Firefox) simply doesn't do this check but implements some native proxy handling not caring of how the system work.

这篇关于Android 应用程序如何决定是否要使用网络代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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