Android判断当前设备是否设置了代理

主要是通过反射读取wifi配置信息,不过6.0开始不能修改代理了,会校验权限,只有系统应用权限可以修改。

WifiManager manager = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = manager.getConnectionInfo();

        List<WifiConfiguration> configs = manager.getConfiguredNetworks();
        if (configs == null) {
            return false;
        }
        for (WifiConfiguration wifiConfiguration : configs) {
            if (wifiConfiguration.SSID.equals("\"" + wifiInfo.getSSID() + "\"")
                    || wifiConfiguration.SSID.equals(wifiInfo.getSSID())) {
                if (Build.VERSION.SDK_INT > 20) {
                    try {
                        Method getProxyMethod = WifiConfiguration.class.getDeclaredMethod("getHttpProxy");
                        if (getProxyMethod != null) {
                            Object state = getProxyMethod.invoke(wifiConfiguration);
                            if (state == null)
                                return false;
                            Field actualhost = ProxyInfo.class.getDeclaredField("mHost");
                            actualhost.setAccessible(true);
                            Object actualhostStr = actualhost.get(state);
                            return actualhostStr != null && actualhostStr.equals(host);
                        }
                    } catch (NoSuchMethodException | IllegalAccessException
                            | IllegalArgumentException
                            | InvocationTargetException
                            e) {
                        return false;

                    } catch (NoSuchFieldException e) {
                        e.printStackTrace();
                        return false;
                    }
                } else if (Build.VERSION.SDK_INT > 14) {
                    try {
                        Field linkPropertiesField = WifiConfiguration.class
                                .getDeclaredField("linkProperties");
                        linkPropertiesField.setAccessible(true);
                        Object linkProperties = linkPropertiesField.get(wifiConfiguration);
                        Class linkPropertiesClass = Class.forName("android.net.LinkProperties");
                        Method getProxy = linkPropertiesClass.getMethod("getHttpProxy");
                        Object proxyProperties = getProxy.invoke(linkProperties);
                        if (proxyProperties == null)
                            return false;
                        Class proxyinfo = Class.forName("android.net.ProxyProperties");

                        Field actualhost = proxyinfo.getDeclaredField("mHost");
                        actualhost.setAccessible(true);
                        Object actualhostStr = actualhost.get(proxyProperties);

                        return actualhostStr != null && actualhostStr.toString().equals(host);


                    } catch (NoSuchFieldException | IllegalAccessException
                            | IllegalArgumentException  e) {

                        return false;
                    } catch (ClassNotFoundException e) {
                        return false;
                    } catch (NoSuchMethodException e) {
                        return false;
                    } catch (InvocationTargetException e) {
                        return false;
                    }
                }
            }

        }

        return false;

还有一种方法是用adb

adb shell dumpsys wifi | grep "Proxy settings: STATIC" -A 1
//如果有代理返回代理信息,否则返回为空,结合下面的命令判断是否是当前激活的wifi
adb shell dumpsys netstats | grep "Active interfaces:" -A 1

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 155,721评论 23 678
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 6,999评论 6 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 119,187评论 16 133
  • 经历了昨天艰难的出柜第一步,今日早上尽然风平浪静,反而让我有点摸不着头脑。。。从家里出来,和大人约会,吃到了大人好...
    禅司大人阅读 66评论 1 1
  • 对于子视图加到父视图这样的操作,作为IOS coder来说一天会写很多次,但是如果二个view不同颜色的话,你知道...
    TByounger阅读 453评论 0 0