集成了騰訊瀏覽服務(wù)X5內(nèi)核了,發(fā)現(xiàn)手機(jī)沒有裝微信\QQ這些,且沒有wifi的時(shí)候,無法自動加載內(nèi)核。所以騰訊瀏覽服務(wù)SDK就會切換到系統(tǒng)內(nèi)核(無需自己處理)。系統(tǒng)的webview就有很多問題了,例如視頻全屏、最小化需要自己實(shí)現(xiàn)。
這是騰訊瀏覽服務(wù)SDK的方法:
mWebView.setWebChromeClient(new WebChromeClient(){
/**
* 視頻全屏調(diào)用此函數(shù)
* @param view
* @param customViewCallback
*/
@Override
public void onShowCustomView(View view, int i, IX5WebChromeClient.CustomViewCallback customViewCallback) {
super.onShowCustomView(view, i, customViewCallback);
setFullScreen(view,customViewCallback);
}
/**
* 視頻全屏調(diào)用此函數(shù)
* @param view
* @param customViewCallback
*/
@Override
public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
super.onShowCustomView(view, customViewCallback);
setFullScreen(view,customViewCallback);
}
/**
*退出全屏調(diào)用此函數(shù)
**/
@Override
public void onHideCustomView() {
super.onHideCustomView();
}
});
這是webview自己的方法:
mWebView.setWebChromeClient(new WebChromeClient(){
/**
* 點(diǎn)擊全屏
* @param view
* @param callback
*/
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
}
/**
* 點(diǎn)擊全屏
* @param view
* @param requestedOrientation
* @param callback
*/
@Override
public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
super.onShowCustomView(view, requestedOrientation, callback);
}
/**
* 取消全屏
*/
@Override
public void onHideCustomView() {
super.onHideCustomView();
}
});
兩種的方法沒有什么差異,有X5內(nèi)核就會使用X5的,沒有就使用系統(tǒng)的。
實(shí)現(xiàn)視頻全屏的方式都是一樣
內(nèi)容簡單,直接貼代碼
WebViewActivity.java
退出全屏onHideCustomView()的時(shí)候,videoLayout一定不要removeView(),會導(dǎo)致無法在WebView彈虛擬鍵盤,原因未知。
只能在全屏的時(shí)候判斷videoLayout是否存在有子view,有就清空。
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class WebViewActivity extends AppCompatActivity {
WebView mWebView;
private View nVideoView = null;
private WebChromeClient.CustomViewCallback callback;
private FrameLayout videoLayout;//視頻全屏
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
initView();
}
private void initView() {
videoLayout = findViewById(R.id.video_layout);
mWebView=findViewById(R.id.webview);
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setPluginState(WebSettings.PluginState.ON);
//settings.setPluginsEnabled(true);
settings.setAllowFileAccess(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(view.getUrl());
return super.shouldOverrideUrlLoading(view, request);
}
});
mWebView.setWebChromeClient(new CustomWebViewChromeClient());
mWebView.loadUrl("http://www.pearvideo.com/video_1384268");
}
class CustomWebViewChromeClient extends WebChromeClient{
/**
* 點(diǎn)擊全屏
* @param view
* @param callback
*/
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
setFullScreen(view,callback);
}
/**
* 點(diǎn)擊全屏
* @param view
* @param requestedOrientation
* @param callback
*/
@Override
public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) {
super.onShowCustomView(view, requestedOrientation, callback);
setFullScreen(view,callback);
}
/**
* 取消全屏
*/
@Override
public void onHideCustomView() {
super.onHideCustomView();
if (nVideoView == null) {
return;
}
try {
callback.onCustomViewHidden();
} catch (Exception e) {
}
nVideoView.setVisibility(View.GONE);
//如果在這里remove子view,會導(dǎo)致WebView無法彈出虛擬鍵盤。removeAllViews和removeView()都試過了,找了半天。原因未知
// videoLayout.removeAllViews();
// videoLayout.removeView(nVideoView);
nVideoView = null;
videoLayout.setVisibility(View.GONE);
// 設(shè)置豎屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// 取消全屏
final WindowManager.LayoutParams attrs =getWindow().getAttributes();
attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setAttributes(attrs);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
}
/**
* 設(shè)置全屏
* @param view
* @param customViewCallback
*/
private void setFullScreen(View view, WebChromeClient.CustomViewCallback customViewCallback){
if (nVideoView != null) {
customViewCallback.onCustomViewHidden();
return;
}
nVideoView = view;
nVideoView.setVisibility(View.VISIBLE);
callback = customViewCallback;
//退出全屏的時(shí)候無法remove,會導(dǎo)致WebView無法彈虛擬鍵盤,只能在設(shè)置全屏的時(shí)候清空之前add的
if(videoLayout.getChildCount()>0){
videoLayout.removeAllViews();
}
videoLayout.addView(nVideoView);
videoLayout.setVisibility(View.VISIBLE);
videoLayout.bringToFront();
//設(shè)置橫屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//設(shè)置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
布局 :activity_web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebViewActivity">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
<FrameLayout
android:id="@+id/video_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
清單文件:AndroidManifest.xml
需要設(shè)置切換橫豎屏不會重新加載Activity
android:configChanges="orientation|keyboardHidden|screenSize"
<activity
android:name=".WebViewActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"/>