在 Android 中获取 WiFi 代理设置 [英] Getting WiFi proxy settings in Android

查看:47
本文介绍了在 Android 中获取 WiFi 代理设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取 WIFI 代理设置

I am trying to read WIFI proxy settings

  • 代理主机
  • 代理端口
  • 代理用户(身份验证)
  • 代理密码(身份验证)

来自 android 版本 2.X.X – 4.X.X 中的设备,但没有任何成功.

from devices in android versions 2.X.X – 4.X.X without any success.

呼叫:

String proxy = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.HTTP_PROXY);

总是返回空值.

我还添加到我的 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" />

它仍然返回 null.

still it returns null.

也试过了:

android.net.Proxy. getHost(Context ctx) – which is deprecated – returns the IP
android.net.Proxy. getPortt(Context ctx) – which is deprecated – returns always -1.

Java 调用:

System.getProperty("http.proxyHost");
System.getProperty("http.proxyCall");

也返回空值.

是否有可以检索所有这些设置或至少部分检索所有 android 版本的设备的工作代码?

Is there a working code which retrieves all these settings or at least partially from devices in all android versions?

推荐答案

我找到了这个项目:Android Proxy Library它提供了向后兼容的方式来查询代理设置以及在旧版本的 Android 上为 WebViews 设置它们.

I found this project: Android Proxy Library Which provides backward compatible ways of querying Proxy settings as well as setting them for WebViews on older versions of Android.

    // Grab Proxy settings in a backwards compatible manner
    ProxyConfiguration proxyConfig = ProxySettings.getCurrentHttpProxyConfiguration( context );

    // Set Proxy for WebViews on older versions of Android
    ProxyUtils.setWebViewProxy( getActivity().getApplicationContext() );

但是,您需要了解在 WiFi AP 上设置的代理设置.由于 WiFi 特定的代理设置直到 3.1 才在 Android 中实现,所有公开该功能的 3.1 之前的设备都使用某种自定义 hack.它们不以任何标准方式工作.因此,像这样的库将无法从这些黑客之一中获取任何代理集.

However, there is something you need to understand about Proxy Settings set on a WiFi AP. Since WiFi specific Proxy Settings were not implemented in Android proper until 3.1, all pre-3.1 devices that expose that functionality are using some sort of custom hack. They don't work in any sort of standard way. So libraries like this won't be able to grab any proxy set from one of those hacks.

然而,在 3.1 之前的版本中有一个系统范围的代理,这种库抓取.当然,Android 认为不提供任何官方方式来修改此设置是合适的.但是 Play 商店中有一些应用程序可以让您这样做,这是我正在使用的应用程序:代理设置,它运行良好,设置系统代理并允许您通过此库或什至更简单的方法(例如查询 JVM 代理设置)获取它.

There is however a System Wide Proxy in pre-3.1 that this sort of library WILL grab. Of course Android saw fit not to provide any official way to modify this setting. But there are apps on the Play Store that will allow you to do it, this is the one I'm using: Proxy Settings and it works well, setting the System Proxy and allowing you to grab it either via this library, or even simpler methods like querying the JVM proxy settings.

我最终没有使用 APL,而是采用了更简单的实现:

I ended up not using the APL and instead went with a much simpler implementation:

    private static final boolean IS_ICS_OR_LATER = Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;

    ...

    String proxyAddress;
    int proxyPort;

    if( IS_ICS_OR_LATER )
    {
        proxyAddress = System.getProperty( "http.proxyHost" );

        String portStr = System.getProperty( "http.proxyPort" );
        proxyPort = Integer.parseInt( ( portStr != null ? portStr : "-1" ) );
    }
    else
    {
        proxyAddress = android.net.Proxy.getHost( context );
        proxyPort = android.net.Proxy.getPort( context );
    }

这篇关于在 Android 中获取 WiFi 代理设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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