为了账号安全,请及时绑定邮箱和手机立即绑定

Android WebView 代理设置

Android WebView 代理设置

千巷猫影 2021-12-30 20:32:00
目前有没有办法使用 Android sdk 通过 Android 应用程序设置 WebView 代理设置?
查看完整描述

1 回答

?
慕姐4208626

TA贡献1852条经验 获得超7个赞

    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


查看完整回答
反对 回复 2021-12-30
  • 1 回答
  • 0 关注
  • 473 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信