1.改清单文件
在你放置WebView的Activity的声明中加上以下代码:
android:hardwareAccelerated ="true"
android:configChanges="orientation|screenSize|keyboardHidden"
2.改Java类
1.对WebView进行初始化设置:
@JavascriptInterface
private void initWebView() {
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setSupportZoom(true);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setDefaultTextEncodingName("utf-8");
mWebSettings.setLoadsImagesAutomatically(true);
mWebSettings.setBlockNetworkImage(false);//解决图片不显示
mWebSettings.setTextZoom(100);//设置默认缩放比例,防止网页跟随系统字体大小变化
//待定项目@{
mWebSettings.setAllowFileAccess(true);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
mWebSettings.setDomStorageEnabled(true);// 必须保留,否则无法播放优酷视频,其他的OK
try {
if (Build.VERSION.SDK_INT >= 16) {
Class clazz = mWebSettings.getClass();
Method method = clazz.getMethod("setAllowUniversalAccessFromFileURLs", boolean.class);
if (method != null) {
method.invoke(mWebSettings, true);
}
}
} catch (Exception e) {
e.printStackTrace();
}
mWebSettings.setPluginState(WebSettings.PluginState.ON);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
//@}
//调用JS方法.安卓版本大于17,加上注解 @JavascriptInterface
mWebSettings.setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mWebSettings.setMixedContentMode(android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
mWebView.setWebChromeClient(webChromeClient);
}
标明待定项目的属性设置可有可无,不影响本文说的功能实现。智汇代理申请
2.实现chrome代理:
WebChromeClient webChromeClient = new WebChromeClient() {
WebChromeClient.CustomViewCallback mCallback;
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
fullScreen();
mCallback = callback;
super.onShowCustomView(view, callback);
}
@Override
public void onHideCustomView() {
fullScreen();
super.onHideCustomView();
}
};
private void fullScreen() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
LogUtil.i(TAG+" 横屏");
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
LogUtil.i(TAG+" 竖屏");
}
}
3.重写onConfigurationChanged方法:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
switch (newConfig.orientation) {
case Configuration.ORIENTATION_LANDSCAPE:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
break;
case Configuration.ORIENTATION_PORTRAIT:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
break;
}
}
OK,按照以上步骤粘贴完之后就大功告成了!你可以在直播平台里复制一个链接到该页面去打开,然后点击全屏观看直播测试一下。