WebView使用问题

线程

AOS要求WebView的调用线程必须一致,否则会抛出异常,提示"All WebView methods must be called on the same thread. "。WebView在初始化时,mWebViewThread会记录当前的初始化线程,因为webview是一个UI控件,我们一般会在主线程中初始化,所以很多情况下这个成员变量就是当前的主线程。之后,在调用webview的相关方法时,都会首先调用checkThread()方法检测线程是否一致,即检测两个线程的looper对象。如果不相同,会有异常抛出。

    private final Looper mWebViewThread = Looper.myLooper();

    private void checkThread() {
        // Ignore mWebViewThread == null because this can be called during in the super class
        // constructor, before this class's own constructor has even started.
        if (mWebViewThread != null && Looper.myLooper() != mWebViewThread) {
            Throwable throwable = new Throwable(
                    "A WebView method was called on thread '" +
                    Thread.currentThread().getName() + "'. " +
                    "All WebView methods must be called on the same thread. " +
                    "(Expected Looper " + mWebViewThread + " called on " + Looper.myLooper() +
                    ", FYI main Looper is " + Looper.getMainLooper() + ")");
            Log.w(LOGTAG, Log.getStackTraceString(throwable));
            StrictMode.onWebViewMethodCalledOnWrongThread(throwable);

            if (sEnforceThreadChecking) {
                throw new RuntimeException(throwable);
            }
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,645评论 25 708
  • 1、设置背景颜色时没有效果 一直白色 下列代码在模拟器上面试的时候是好的,可以在真机上跑的时候就不是了,得到的是一...
    proud2008阅读 476评论 0 0
  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,376评论 11 349
  • 2016年过去了,干的大事只考一个驾照,还差科四没考,不能再拖了四号马上去考。2017年减肥20斤,目前55千克多...
    减肥加油阅读 123评论 0 0
  • 要想成功,你得学会早起,跑步,读书,自律,独处,思考,行动……
    忧郁的老虎阅读 316评论 3 7