美文网首页
Android中系统应用对WebView的适配

Android中系统应用对WebView的适配

作者: 沐络 | 来源:发表于2019-07-18 11:16 被阅读0次

    Android 4.4 开始修改了浏览器的内核导致从5.0 开始,如果App为系统应用或者Root用户,将无法使用WebView进行加载网页的操作

    错误日志如下:

    java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes

    针对错误修改方法:

    1,替换WebView  如果只支持简单的

    2,可以尝试分离把引用系统权限的部分分离成独立的app进行独立维护

    3, 如果是定制版系统可以修改FrameWork层,注释掉相应的异常

    public static void hookWebView() {

            int sdkInt = Build.VERSION.SDK_INT;

            try {

                Class<?> factoryClass = Class.forName("android.webkit.WebViewFactory");

                Field field = factoryClass.getDeclaredField("sProviderInstance");

                field.setAccessible(true);

                Object sProviderInstance = field.get(null);

                if (sProviderInstance != null) {

                    Log.i("thi","sProviderInstance isn't null");

                    return;

                }

                Method getProviderClassMethod;

                if (sdkInt > 22) {

                    getProviderClassMethod = factoryClass.getDeclaredMethod("getProviderClass");

                } else if (sdkInt == 22) {

                    getProviderClassMethod = factoryClass.getDeclaredMethod("getFactoryClass");

                } else {

                    return;

                }

                getProviderClassMethod.setAccessible(true);

                Class<?> providerClass = (Class<?>) getProviderClassMethod.invoke(factoryClass);

                Class<?> delegateClass = Class.forName("android.webkit.WebViewDelegate");

                Constructor<?> providerConstructor = providerClass.getConstructor(delegateClass);

                if (providerConstructor != null) {

                    providerConstructor.setAccessible(true);

                    Constructor<?> declaredConstructor = delegateClass.getDeclaredConstructor();

                    declaredConstructor.setAccessible(true);

                    sProviderInstance = providerConstructor.newInstance(declaredConstructor.newInstance());

                    field.set("sProviderInstance", sProviderInstance);

                }

            } catch (Throwable e) {

                e.printStackTrace();

            }

        }

    相关文章

      网友评论

          本文标题:Android中系统应用对WebView的适配

          本文链接:https://www.haomeiwen.com/subject/uuiklctx.html