博客转载请标明出处: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'}
}
2、关联crosswalk库
compile(name: 'crosswalk-23.53.589.4', ext: 'aar')
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
WebView中的setwebViewClient(webViewClient)对应XWalkView中的setResourceClient(new XWalkResourceClient(mWebView)方法
mWebView.setResourceClient(new XWalkResourceClient(mWebView) {//=========HTML5定位==========================================================//需要先加入权限//<uses-permission android:name="android.permission.INTERNET"/>//<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>//<uses-permission android:name=// "android.permission.ACCESS_COARSE_LOCATION"/>@Overridepublic boolean shouldOverrideUrlLoading(XWalkView view, String url) {view.loadUrl(url);return true;}@Overridepublic void onReceivedSslError(XWalkView view, ValueCallback<Boolean> callback, SslError error) {super.onReceivedSslError(view, callback, error);}@Overridepublic void onLoadFinished(XWalkView view, String url) {super.onLoadFinished(view, url);}@Overridepublic void onLoadStarted(XWalkView view, String url) {super.onLoadStarted(view, url);}@Overridepublic void onProgressChanged(XWalkView view, int progressInPercent) {super.onProgressChanged(view, progressInPercent);}@Overridepublic void onReceivedClientCertRequest(XWalkView view, ClientCertRequest handler) {super.onReceivedClientCertRequest(view, handler);}@Overridepublic void onDocumentLoadedInFrame(XWalkView view, long frameId) {super.onDocumentLoadedInFrame(view, frameId);}@Overridepublic void onReceivedHttpAuthRequest(XWalkView view, XWalkHttpAuthHandler handler, String host, String realm) {super.onReceivedHttpAuthRequest(view, handler, host, realm);}@Overridepublic void onReceivedLoadError(XWalkView view, int errorCode, String description, String failingUrl) {super.onReceivedLoadError(view, errorCode, description, failingUrl);}@Overridepublic void onReceivedResponseHeaders(XWalkView view, XWalkWebResourceRequest request, XWalkWebResourceResponse response) {super.onReceivedResponseHeaders(view, request, response);}});
WebView中的setWebChromeClient(webChromeClient)对应XWalkView中的setUIClient(new XWalkUIClient(mWebView)方法
mWebView.setUIClient(new XWalkUIClient(mWebView) {@Overridepublic void onPageLoadStarted(XWalkView view, String url) {super.onPageLoadStarted(view, url);}@Overridepublic boolean onJsAlert(XWalkView view, String url, String message, XWalkJavascriptResult result) {return super.onJsAlert(view, url, message, result);}@Overridepublic void onScaleChanged(XWalkView view, float oldScale, float newScale) {super.onScaleChanged(view, oldScale, newScale);}@Overridepublic void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {super.onPageLoadStopped(view, url, status);}});
当然为了避免关闭xWalkView引起内存泄露的问题,我们选择了动态加载这个view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mWebView = new XWalkView(getApplicationContext());
mWebView.setLayoutParams(params);
mLayout.addView(mWebView);
因为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);
}
Demo还有网络监听NetReceiver和来电监听PhoneCallReceiver,这里不再赘述,感兴趣可以查看Demo源码
博客转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/73105712
源码下载:http://download.csdn.net/detail/nmyangmo/9868127