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

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

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

        List 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判断当前设备是否设置了代理)