欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

个人整理的android webview的一些适配方案:

程序员文章站 2022-06-21 19:27:04
webview的适配方案:1.支持js:settings.setJavaScriptEnabled(true);2.跳转到外部浏览器下载: webView.setDownloadListener(new DownloadListener() { @Override public void onDownloadStart(String s, String s1, String s2, String s3, long l)...

webview的适配方案:

	1.支持js:settings.setJavaScriptEnabled(true);
	2.跳转到外部浏览器下载:
 webView.setDownloadListener(new DownloadListener()
        {
            @Override
            public void onDownloadStart(String s, String s1, String s2, String s3, long l)
            {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addCategory(Intent.CATEGORY_BROWSABLE);
                intent.setData(Uri.parse(s));
                context.startActivity(intent);
            }
        });
	3.显示https的图片:
//1
  if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP)
  {
            webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
  }

//2.
webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view,
                    SslErrorHandler handler, SslError error) {
                // TODO Auto-generated method stub
                // handler.cancel();// Android默认的处理方式
                handler.proceed();// 接受所有网站的证书
                // handleMessage(Message msg);// 进行其他处理
            }
});
	4.微信支付提示商家参数异常,需要设置referer:
 @Override
    public boolean shouldOverrideUrlLoading(final WebView view, String url)
    {
    			//用自己的方式获取referer
           		String referer = WxPayRefererUtils.getPayRefererUrl(url);
            
                Map<String, String> extraHeaders = new HashMap<String, String>();
                extraHeaders.put("Referer", referer );
                view.loadUrl(url, extraHeaders);
	 }


	5.支持上传文件,图片等:需要重写 openFileChooser 方法
	
	6.调用网页中的js方法:webview.loadUrl("javascript:shareApp()");   shareApp为方法名

	7.遍历网页中的标签并且设置点击事件:
WebViewClient myClient = new WebViewClient()
        {
            @Override
            public void onPageFinished(WebView webView, String s)
            {
                super.onPageFinished(webView, s);
                webView.loadUrl("javascript:(function(){" +
                        "var objs = document.getElementsByTagName(\"img\"); " +
                        "for(var i=0;i<objs.length;i++) " +
                        "{"
                        + " objs[i].οnclick=function() " +
                        " { "
                        + "  window.imagelistener.openImage(this.src); " +
                        " } " +
                        "}" +
                        "})()"); 
            }
        };
   public class JavascriptInterface
    {
        @android.webkit.JavascriptInterface
        public void openImage(String imageUrl)
        {
            Intent intent = new Intent();
            intent.putExtra("image_path", imageUrl);
            startActivity(intent);
        }
      
    }

	8.android 10.0 出现

java.lang.RuntimeException: Using WebView from more than one process at once with the same data directory is not supported. https://crbug.com/558377
        at org.chromium.android_webview.AwBrowserProcess.b(PG:12)
        at n6.m(PG:33)
        at m6.run(PG:2)
        at org.chromium.base.task.TaskRunnerImpl.g(PG:11)
        at Nt.run(Unknown Source:2)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:6878)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)

	的问题。网上说在application里面配置如下信息,自己手上10.0的机器没出现过这样的问题,但是极光统计上显示有这样的问题,先记录一下,没这样改过,不知道行不行。
public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        initWebViewDataDirectory(this);
    }

/**
* 得到进程名称
* @param context
* @return
*/
public static String getProcessName(Context context) {
    try {
            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;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
}

/**
* 为webView设置目录后缀
* @param context
*/
@RequiresApi(api = Build.VERSION_CODES.P)
public static void initWebViewDataDirectory(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        String processName = getProcessName(context);
        if (!context.getPackageName().equals(processName)) {//判断是否是默认进程名称
            WebView.setDataDirectorySuffix(processName);
        }
    }
}

欢迎补充

本文地址:https://blog.csdn.net/qq_32503241/article/details/107928262

相关标签: android webview