最近做项目遇到webview加载视频 全屏,下载,https问题 特此总结了一下
1.在layout文件里放入一个WebView控件和一个显示视频控件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/video_fullView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
2.声明
private FrameLayout video_fullView;
private View xCustomView;
private WebView webView;
private myWebChromeClient xwebchromeclient;
private WebChromeClient.CustomViewCallback xCustomViewCallback;
private String url = "https://gyxb.hbwwcc.com/wsxb/business/knowledge_view2.htm?knowledgeId=20150727133126394209&userId=USER_20161129095011349993";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityt_webview);
initView();
initWebViewSettings();
initWebView();
}
实例化控件
private void initView() {
video_fullView = (FrameLayout) findViewById(R.id.video_fullView);
webView = (WebView) findViewById(R.id.webview);
}
/**
* 初始化网络数据
*/
private void initWebView() {
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
System.out.println("网络连接失败 ,请检查网络!");
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // 接受所有网站的证书 加载https网址时需加上这句 否则会出现空白现象
System.out.println("onReceivedSslError======" + error);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
webView.loadUrl(url);
webView.setDownloadListener(new MyWebViewDownLoadListener(this));//点击链接下载文档,,
xwebchromeclient = new myWebChromeClient();
webView.setWebChromeClient(xwebchromeclient);
}
/**
* 初始化网络设置
*/
private void initWebViewSettings() {
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);//设置支持页面js可用
webSettings.setBuiltInZoomControls(true);
webSettings.setUseWideViewPort(true);// 可任意比例缩放
webSettings.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。
webSettings.setSavePassword(true);
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
//设置下4.4以上的webview,允许加载混合内容,https加载时候需加上这行代码
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
}
public class myWebChromeClient extends WebChromeClient {
private View xprogressvideo;
// 播放网络视频时全屏会被调用的方法
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
webView.setVisibility(View.INVISIBLE);
// 如果一个视图已经存在,那么立刻终止并新建一个
if (xCustomView != null) {
callback.onCustomViewHidden();
return;
}
video_fullView.addView(view);
xCustomView = view;
xCustomViewCallback = callback;
video_fullView.setVisibility(View.VISIBLE);
}
// 视频播放退出全屏会被调用的
@Override
public void onHideCustomView() {
if (xCustomView == null)// 不是全屏播放状态
return;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
xCustomView.setVisibility(View.GONE);
video_fullView.removeView(xCustomView);
xCustomView = null;
video_fullView.setVisibility(View.GONE);
xCustomViewCallback.onCustomViewHidden();
webView.setVisibility(View.VISIBLE);
}
// 视频加载时进程loading
@Override
public View getVideoLoadingProgressView() {
return xprogressvideo;
}
}
```
/**
* 判断是否是全屏
*
* @return
*/
public boolean inCustomView() {
return (xCustomView != null);
}
/**
* 全屏时按返加键执行退出全屏方法
*/
public void hideCustomView() {
xwebchromeclient.onHideCustomView();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onResume() {
super.onResume();
System.out.println("onResume()===== ");
try {
webView.getClass().getMethod("onResume").invoke(webView, (Object[]) null);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
/**
* 设置为横屏
*/
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
/**
* 点击返回键时候 判断是否全屏播放视频
*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if (inCustomView()) {
// webViewDetails.loadUrl("about:blank");
hideCustomView();
return true;
} else if (webView.canGoBack()) {
webView.goBack(); // goBack()表示返回WebView的上一页面
return true;
} else {
finish();
}
}
return super.onKeyDown(keyCode, event);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected void onPause() {
System.out.println("onPause()===== ");
try {
webView.getClass().getMethod("onPause").invoke(webView, (Object[]) null);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
super.onPause();
}
```
/**
* webView下载类
*/
public class MyWebViewDownLoadListener implements DownloadListener {
private Context mContext;
public MyWebViewDownLoadListener(Context mContext){
this.mContext=mContext;
}
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
mContext.startActivity(intent);
}
}
//解决图片 过大显示问题 从新定义宽高
private void imgReset() {
progress_web.loadUrl("javascript:(function(){"
+ "var objs = document.getElementsByTagName('img'); "
+ "for(var i=0;i<objs.length;i++) " + "{"
+ "var img = objs[i]; "
+ " img.style.width = '100%'; "
+ " img.style.height = 'auto'; "
+ "}" + "})()");
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
imgReset();
}