webview创建过程分析(一)

我们知道在使用webview的时候需要先创建一个weview,即

WebView webView = new WebView(context)

今天我们通过源码来分析这个WebView的创建过程。当我们使用上面的代码创建webview之后,会调用到下面WebView的构造方法中,这里除了context参数之后其他的都是默认值

protected WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes,
            Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) {
        super(context, attrs, defStyleAttr, defStyleRes);

        // WebView is important by default, unless app developer overrode attribute.
        if (getImportantForAutofill() == IMPORTANT_FOR_AUTOFILL_AUTO) {
            setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_YES);
        }

        if (context == null) {
            throw new IllegalArgumentException("Invalid context argument");
        }
        sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >=
                Build.VERSION_CODES.JELLY_BEAN_MR2;

        //1.线程检查
        checkThread();

        //2.创建WebViewProvider
        ensureProviderCreated();

        //3.初始化WebViewProvider
        mProvider.init(javaScriptInterfaces, privateBrowsing);
        // Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed.
        CookieSyncManager.setGetInstanceIsAllowed();
    }

从上面的创建过程可以知道这里主要分3步

  1. 进行线程检查
  2. 初始化WebViewProvider
  3. 初始化这个provider。

接下来我们仔细分析下第二和第三步

创建WebViewProvider

我们看下创建WebViewProvider的源码过程,也就是WebView中ensureProviderCreated()方法

private void ensureProviderCreated() {
        checkThread();
        if (mProvider == null) {
            // As this can get called during the base class constructor chain, pass the minimum
            // number of dependencies here; the rest are deferred to init().

            //主要过程分为两步
            //1.getFactory
            //2.createWebView
            mProvider = getFactory().createWebView(this, new PrivateAccess());
        }
    }
private static WebViewFactoryProvider getFactory() {
        return WebViewFactory.getProvider();
    }

这里会调用WebViewFactory 的getProvider方法:

  • getProv
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值