收藏 分销(赏)

Cordova-Android源码分析系列二(CordovaWebView相关类分析).docx

上传人:仙人****88 文档编号:11814792 上传时间:2025-08-14 格式:DOCX 页数:8 大小:46.97KB 下载积分:10 金币
下载 相关 举报
Cordova-Android源码分析系列二(CordovaWebView相关类分析).docx_第1页
第1页 / 共8页
Cordova-Android源码分析系列二(CordovaWebView相关类分析).docx_第2页
第2页 / 共8页


点击查看更多>>
资源描述
本篇文章是Cordova Android源码分析系列文章的第二篇,主要分析CordovaWebView和CordovaWebViewClient类,通过分析代码可以知道Web网页加载的过程,错误出来,多线程处理等。 CordovaWebView类分析     CordovaWebView类继承了Android WebView类,这是一个很自然的实现,共1000多行代码。包含了PluginManager pluginManager,BroadcastReceiver receiver,CordovaInterface cordova, CordovaWebViewClient viewClient,CordovaChromeClient chromeClient,NativeToJsMessageQueue jsMessageQueue ,ExposedJsApi exposedJsApi,CordovaResourceApi resourceApi等重要的成员变量,与其它核心类关联起来。     提供了4个构造函数:CordovaWebView(Context context),CordovaWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) ,分别对应Android WebView类的相应构造函数。这些构造函数首先调用WebView的相应构造函数,然后初始化cordova类变量,按情况依次调用自身的setWebChromeClient,initWebViewClient,loadConfiguration,setup方法。     setWebChromeClient方法设置WebChromeClient,调用了WebView类的setWebChromeClient方法。     initWebViewClient方法根据Android SDK版本的不同,分别调用setWebViewClient,针对IceCreamSandwich版本,调用setWebViewClient(new IceCreamCordovaWebViewClient(this.cordova, this))。暂时不知道具体的原因。 [java] view plain copy 1. /**  2.  * set the WebViewClient, but provide special case handling for IceCreamSandwich.  3.  */   4. private void initWebViewClient(CordovaInterface cordova) {   5.     if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB ||   6.             android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.JELLY_BEAN_MR1)   7.     {   8.         this.setWebViewClient(new CordovaWebViewClient(this.cordova, this));   9.     }   10.     else   11.     {   12.         this.setWebViewClient(new IceCreamCordovaWebViewClient(this.cordova, this));   13.     }   14. }       setup方法的作用是初始化WebView。首先启用JavaScript,就像我们自己使用WebView时一样:    [java] view plain copy 1. WebSettings settings = this.getSettings();   2. settings.setJavaScriptEnabled(true);   3. settings.setJavaScriptCanOpenWindowsAutomatically(true);   4. settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);            针对HTC 2.x devices系列设备设置nav dump(针对ICS禁用,针对Jellybean 4.2降级),具体实现是通过反射拿到setNavDump方法,如果设备型号中包含HTC并且SDK版本小于11(HONEYCOMB),设置setNavDump(true)。     设置不保存网页表单数据,大家可以放心了!         //We don't save any form data in the application         settings.setSaveFormData(false);         settings.setSavePassword(false);    后面是设置databasePath,是否打开调试模式等,依次调用了setDomStorageEnabled,setGeolocationEnabled,setAppCacheMaxSize,setAppCachePath,setAppCacheEnabled等WebSetting的方法,从名字就可以很容易的理解作用。    然后注册了一个receiver,监听的IntentFilter action是ACTION_CONFIGURATION_CHANGED。    最后初始化了pluginManager,jsMessageQueue,exposedJsApi,resourceApi成员变量。    下面我们看下Web页面载入的流程,先看loadUrl方法,它其实是调用了loadUrlIntoView方法。loadUrlIntoView方法首先会初始化插件管理器pluginManager,然后创建了2个Runnable对象loadError和timeoutCheck,loadError用于通知客户端viewClient错误信息,timeoutCheck用于检查页面超时,最后在UI线程中调用loadUrlNow(url)。注意timeoutCheck任务是在线程池中运行的。loadUrlNow方法最终调用了WebView的loadUrl(url)方法。     [java] view plain copy 1. // Load url   2. this.cordova.getActivity().runOnUiThread(new Runnable() {   3.     public void run() {   4.         cordova.getThreadPool().execute(timeoutCheck);   5.         me.loadUrlNow(url);   6.     }   7. });   CordovaWebViewClient类分析     CordovaWebViewClient类继承了android WebViewClient,实现了CordovaWebView的回掉函数,这些回掉函数在渲染文档的过程中会被触发,例如onPageStarted(),shouldOverrideUrlLoading()等方法。     方法public boolean shouldOverrideUrlLoading(WebView view, String url) 为上层的web应用提供了url加载时处理的机会,js中的exec方法会被拦截,交给handleExecUrl(url)方法处理。如果uri是以tel,sms,geo,market等开头,这里会通过Intent启用相关的App处理。如果是我们自己App或文件,则会启动一个新的Activity,包含一个新的CordovaWebView,主要当按返回键时,可以返回我们的应用。     方法public void onPageStarted(WebView view, String url, Bitmap favicon) 通知应用页面开始加载,如果页面中包含frameset或iframe,这些嵌入的页面加载时是不会触发onPageStarted的。通过this.appView.postMessage方法发送通知给所有插件。    [java] view plain copy 1. @Override   2.     public void onPageStarted(WebView view, String url, Bitmap favicon) {   3.         super.onPageStarted(view, url, favicon);   4.         isCurrentlyLoading = true;   5.         LOG.d(TAG, "onPageStarted(" + url + ")");   6.         // Flush stale messages.   7.         this.appView.jsMessageQueue.reset();   8.    9.         // Broadcast message that page has loaded   10.         this.appView.postMessage("onPageStarted", url);   11.    12.         // Notify all plugins of the navigation, so they can clean up if necessary.   13.         if (this.appView.pluginManager != null) {   14.             this.appView.pluginManager.onReset();   15.         }   16.     }       方法public void onPageFinished(WebView view, String url) 的实现类似onPageStarted,需要注意的是页面加载完成后延时2秒才会停止进度条,目的是防止出现js错误或cordova没有初始化的情况,做法是创建一个线程,sleep 2s,然后在UI线程发送通知spinner stop给所有插件。   CordovaResourceApi类分析 Cordova Android使用okhttp框架作为网络访问基础框架,okhttp 是一个 Java 的 HTTP+SPDY 客户端开发包,同时也支持 Android。 CordovaResourceApi封装了okhttp,主要提供了3个功能: 1.读写Url的助手方法 例如处理assets, resources, content providers, files, data URIs, http[s] 可以用来查询文件类型和内容长度 2.允许插件重定向Url 3.通过createHttpConnection()方法暴露Cordova自带的okhttp库 这个类比较简单,先是创建了静态变量OkHttpClient httpClient和jsThread,这样整个应用只有一个okhttp实例,是轻量级实现。 然后构造函数是 public CordovaResourceApi(Context context, PluginManager pluginManager) {         this.contentResolver = context.getContentResolver();         this.assetManager = context.getAssets();         this.pluginManager = pluginManager;    } 可以看到初始化了contentResolver,assetManager和pluginManager这个类成员变量。 public Uri remapUri(Uri uri) {         assertNonRelative(uri);         Uri pluginUri = pluginManager.remapUri(uri);         return pluginUri != null ? pluginUri : uri;     } 重定向Url的方法,url必须是绝对路径,最终实现在pluginManager.remapUri(uri) [java] view plain copy 1. @Override   2.     public void onPageFinished(WebView view, String url) {   3.         super.onPageFinished(view, url);   4.         // Ignore excessive calls.   5.         if (!isCurrentlyLoading) {   6.             return;   7.         }   8.         isCurrentlyLoading = false;   9.         LOG.d(TAG, "onPageFinished(" + url + ")");   10.    11.         /**  12.          * Because of a timing issue we need to clear this history in onPageFinished as well as  13.          * onPageStarted. However we only want to do this if the doClearHistory boolean is set to  14.          * true. You see when you load a url with a # in it which is common in jQuery applications  15.          * onPageStared is not called. Clearing the history at that point would break jQuery apps.  16.          */   17.         if (this.doClearHistory) {   18.             view.clearHistory();   19.             this.doClearHistory = false;   20.         }   21.    22.         // Clear timeout flag   23.         this.appView.loadUrlTimeout++;   24.    25.         // Broadcast message that page has loaded   26.         this.appView.postMessage("onPageFinished", url);   27.    28.         // Make app visible after 2 sec in case there was a JS error and Cordova JS never initialized correctly   29.         if (this.appView.getVisibility() == View.INVISIBLE) {   30.             Thread t = new Thread(new Runnable() {   31.                 public void run() {   32.                     try {   33.                         Thread.sleep(2000);   34.                         cordova.getActivity().runOnUiThread(new Runnable() {   35.                             public void run() {   36.                                 appView.postMessage("spinner", "stop");   37.                             }   38.                         });   39.                     } catch (InterruptedException e) {   40.                     }   41.                 }   42.             });   43.             t.start();   44.         }   45.    46.         // Shutdown if blank loaded   47.         if (url.equals("about:blank")) {   48.             appView.postMessage("exit", null);   49.         }   50.     }           public File mapUriToFile(Uri uri) 返回url指向的文件,url可以是file://或content格式,这个方法必须运行的后台线程的断言,不能运行在UI线程和WebCore线程。        方法public OpenForReadResult openForRead(Uri uri, boolean skipThreadCheck) throws IOException 用来打开指定的uri,skipThreadCheck设置是否检查是否在后台线程运行,返回值OpenForReadResult是个静态内部类,提供了uri,inputStream,mimeType,内容长度length,AssetFileDescriptor参数,方便我们使用。       下篇文章分析Cordova插件架构,主要涉及CordovaPlugin和PluginManager类。
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服