Crosswalk基本用法
博客转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/73105712
Android 5.0明确指出其webview是支持webRTC的,但是在国内各大厂商的定制下。。咳咳,算了,说多了都是泪啊~!
一怒之下我的webview内核换成了crosswalk。换完之后直接被暴击,APP体积增加了40M啊!!!
不过没办法了webRTC功能和好处还是很诱人啊,这里就不多说了。让我们看crosswalk吧。
Crosswalk官方网址: https://crosswalk-project.org/
Crosswalk 下载地址: https://crosswalk-project.org/documentation/downloads.html
或者: https://download.01.org/crosswalk/releases/crosswalk/
**我下载的是: crosswalk-23.53.589.4.aar 这个版本,兼容x86和arm类型的CPU.
集成方法:1、设置grade外部库为libs,拷贝aar文件到libs**
repositories { flatDir {dirs'libs'}}
1
2
3
4
5
2、关联crosswalk库
compile(name:'crosswalk-23.53.589.4', ext:'aar')
1
crosswalk的使用方法基本和webview一样,只不过叫XWalkView
需要注意的是需要继承XWalkActivity
重写oncreate(),onXWalkReady()方法。加载网页和配置都在crosswalk准备好后进行(onXWalkReady方法中)。关于XWalkView配置和webview基本相同,可以参考webview对XWalkView进行配置
XWalXWalkSettings mWebSettings = mWebView.getSettings(); mWebSettings.setSupportZoom(true);//支持缩放mWebSettings.setBuiltInZoomControls(true);//可以任意缩放mWebSettings.setLoadWithOverviewMode(true); mWebSettings.setUseWideViewPort(true);////将图片调整到适合webview的大小// mWebSettings.setDefaultTextEncodingName("utf-8");mWebSettings.setLoadsImagesAutomatically(true);// mWebSettings.setMixedContentMode()//调用JS方法.安卓版本大于17,加上注解 @JavascriptInterfacemWebSettings.setJavaScriptEnabled(true);//支持JS
1
2
3
4
5
6
7
8
9
10
WebView中的setwebViewClient(webViewClient)对应XWalkView中的setResourceClient(new XWalkResourceClient(mWebView)方法
mWebView.setResourceClient(newXWalkResourceClient(mWebView) {//=========HTML5定位==========================================================//需要先加入权限//////@OverridepublicbooleanshouldOverrideUrlLoading(XWalkView view, String url) { view.loadUrl(url);returntrue; }@OverridepublicvoidonReceivedSslError(XWalkView view, ValueCallback callback, SslError error) {super.onReceivedSslError(view, callback, error); }@OverridepublicvoidonLoadFinished(XWalkView view, String url) {super.onLoadFinished(view, url); }@OverridepublicvoidonLoadStarted(XWalkView view, String url) {super.onLoadStarted(view, url); }@OverridepublicvoidonProgressChanged(XWalkView view,intprogressInPercent) {super.onProgressChanged(view, progressInPercent); }@OverridepublicvoidonReceivedClientCertRequest(XWalkView view, ClientCertRequest handler) {super.onReceivedClientCertRequest(view, handler); }@OverridepublicvoidonDocumentLoadedInFrame(XWalkView view,longframeId) {super.onDocumentLoadedInFrame(view, frameId); }@OverridepublicvoidonReceivedHttpAuthRequest(XWalkView view, XWalkHttpAuthHandler handler, String host, String realm) {super.onReceivedHttpAuthRequest(view, handler, host, realm); }@OverridepublicvoidonReceivedLoadError(XWalkView view,interrorCode, String description, String failingUrl) {super.onReceivedLoadError(view, errorCode, description, failingUrl); }@OverridepublicvoidonReceivedResponseHeaders(XWalkView view, XWalkWebResourceRequest request, XWalkWebResourceResponse response) {super.onReceivedResponseHeaders(view, request, response); } } );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
WebView中的setWebChromeClient(webChromeClient)对应XWalkView中的setUIClient(new XWalkUIClient(mWebView)方法
mWebView.setUIClient(newXWalkUIClient(mWebView) {@OverridepublicvoidonPageLoadStarted(XWalkView view, String url) {super.onPageLoadStarted(view, url); }@OverridepublicbooleanonJsAlert(XWalkView view, String url, String message, XWalkJavascriptResult result) {returnsuper.onJsAlert(view, url, message, result); }@OverridepublicvoidonScaleChanged(XWalkView view,floatoldScale,floatnewScale) {super.onScaleChanged(view, oldScale, newScale); }@OverridepublicvoidonPageLoadStopped(XWalkView view, String url, LoadStatus status) {super.onPageLoadStopped(view, url, status); } });
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
当然为了避免关闭xWalkView引起内存泄露的问题,我们选择了动态加载这个view.
LinearLayout.LayoutParamsparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);mWebView = new XWalkView(getApplicationContext());mWebView.setLayoutParams(params);mLayout.addView(mWebView);
1
2
3
4
因为webRTC需要录音我们还需要动态的申请权限(查看Demo)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO},3332);} else { // mWebView.loadUrl(mUrl);}} else { mWebView.loadUrl(mUrl);}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Demo还有网络监听NetReceiver和来电监听PhoneCallReceiver,这里不再赘述,感兴趣可以查看Demo源码
博客转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/73105712