Android webview 所遇到的坑

安卓原生开发用webview感触颇深,为什么呢 因为有太多的坑要去填 慢慢细说:

1、安卓5.0以上使用https的链接如果里面有http的图片有些机型不能显示图片此时要设置:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }

2、java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. 安卓10 不同的进程使用webview报的错意思是不能在不同的进程使用。在Application里面设置

 private void initWebView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName(this);
            if (!"com.lewanjia.dancelog".equals(processName)){//判断不等于默认进程名称
                WebView.setDataDirectorySuffix(processName);}
        }
    }
    public  String getProcessName(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return processInfo.processName;
            }
        }
        return null;
    }

3、不能调用阿里云的接口。

通过查找发现是因为设置setUserAgentString的时候没有getUserAgentString直接设置了自定义的文字。解决就是先获取自带的然后拼上我们自己定义的就可以了。

4、视频不能全屏 这个就是要设置


        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            windowManager.addView(view, new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION));
            // 去除状态栏和导航按钮
            fullScreen(view);
            fullScreenView = view;
        }

        @Override
        public void onHideCustomView() {
            windowManager.removeViewImmediate(fullScreenView);
            fullScreenView = null;
        }

网上太多就不重复了,

5、选照片只能选一次。

private ValueCallback mUploadMessage;
private ValueCallback mUploadCallbackAboveL;

这些操作完之后要设置为空否则不能进行第二次操作。

6、内存溢出

动态添加webview ,activity生命周期结束时移除。

7、安卓5.x部分手机在用Androidx的时候会报

  • android.content.res.Resources$NotFoundException: String resource ID #0x2040003
  • 尤其是vivo手机不知道什么鬼vivo老是搞特殊还有
    image/*;video/*这种就识别不了别的手机都行,它只能识别一个,那只能一个一个去加咯。 
  • 上面那个解决方案就是自定义webview去解决了,如下
    public class CustomWebView extends WebView {
        public CustomWebView(Context context) {
            super(getFixedContext(context));
        }
    
        public CustomWebView(Context context, AttributeSet attrs) {
            super(getFixedContext(context), attrs);
        }
    
        public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(getFixedContext(context), attrs, defStyleAttr);
        }
    
        public static Context getFixedContext(Context context) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                return context.createConfigurationContext(new Configuration());
            } else {
                return context;
            }
        }
    }

    网上还有用

    androidx.appcompat:appcompat:1.0.2去替代androidx.appcompat:appcompat:1.1.0

你可能感兴趣的:(安卓)