Android WebView 代理设置

目前有没有办法使用 Android sdk 通过 Android 应用程序设置 WebView 代理设置?


千巷猫影
浏览 474回答 1
1回答

慕姐4208626

    Log.d(LOG_TAG, "Setting proxy with >= 4.4 API.");    Context appContext = webView.getContext().getApplicationContext();    System.setProperty("http.proxyHost", host);    System.setProperty("http.proxyPort", port + "");    System.setProperty("https.proxyHost", host);    System.setProperty("https.proxyPort", port + "");    try {        Field loadedApkField = appContext.getClass().getField("mLoadedApk");        loadedApkField.setAccessible(true);        Object loadedApk = loadedApkField.get(appContext);        Class loadedApkCls = Class.forName("android.app.LoadedApk");        Field receiversField = loadedApkCls.getDeclaredField("mReceivers");        receiversField.setAccessible(true);        ArrayMap receivers = (ArrayMap) receiversField.get(loadedApk);        for (Object receiverMap : receivers.values()) {            for (Object rec : ((ArrayMap) receiverMap).keySet()) {                Class clazz = rec.getClass();                if (clazz.getName().contains("ProxyChangeListener")) {                    Method onReceiveMethod = clazz.getDeclaredMethod("onReceive", Context.class, Intent.class);                    Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);                    onReceiveMethod.invoke(rec, appContext, intent);                }            }        }        Log.d(LOG_TAG, "Setting proxy with >= 4.4 API successful!");        return true;它适用于模拟器中的 Andriod 7.1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java