Android判斷當前裝置是否設定了代理

weixin_34365417發表於2018-03-13

主要是通過反射讀取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

相關文章