1.Android Webview中解決H5的音視頻不能自動(dòng)播放的問題
在android 4.2添加了允許用戶手勢(shì)觸發(fā)音視頻播放接口,該接口默認(rèn)為 true ,即默認(rèn)不允許自動(dòng)播放音視頻,只能是用戶交互的方式由用戶自己促發(fā)播放。
解決:
mWebview.getSettings().setMediaPlaybackRequiresUserGesture(false);
2.WebView加載網(wǎng)頁(yè)不顯示圖片解決辦法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//允許進(jìn)行混合加載
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
有的時(shí)候是網(wǎng)站安全https網(wǎng)站安全證書的問題,需要重寫WebViewClient的onReceivedSslError方法
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
handler.proceed(); // 接受網(wǎng)站證書
}
3.禁止webview跳轉(zhuǎn)外部瀏覽器以及能夠跳轉(zhuǎn)微信支付寶
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
if (url.startsWith("weixin://") //微信
|| url.startsWith("alipays://") //支付寶
|| url.startsWith("mailto://") //郵件
|| url.startsWith("tel://")//電話
//其他自定義的scheme
) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
} catch (Exception e) { //防止crash (如果手機(jī)上沒有安裝處理某個(gè)scheme開頭的url的APP, 會(huì)導(dǎo)致crash)
return true;//沒有安裝該app時(shí),返回true,表示攔截自定義鏈接,但不跳轉(zhuǎn),避免彈出上面的錯(cuò)誤頁(yè)面
}
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
4.webview不能下載apk
解決方法同3 可以跳到外面的瀏覽器下載
if (url.endsWith(".apk")) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
5.webview在頁(yè)面中如何返回
在fragment中
webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
});
在activity中
/**
* 點(diǎn)擊返回鍵返回上一頁(yè)而非退出
*
* @param keyCode
* @param event
* @return
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
6.webview處理加載前progrese以及加載錯(cuò)誤顯示errorview,點(diǎn)擊重新加載.
加載progress的顯示與消失主要在onProgressChanged方法里,自定義errorview,其中該view包含一個(gè)repeatRequest的按鈕,當(dāng)網(wǎng)絡(luò)錯(cuò)誤加載失敗,重新點(diǎn)擊該按鈕webview重新加載。
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//由應(yīng)用處理,webview不處理
try {
if (url.startsWith("weixin://") //微信
|| url.startsWith("alipays://") //支付寶
|| url.startsWith("mailto://") //郵件
|| url.startsWith("tel://")//電話
|| url.endsWith(".apk")
|| (!url.startsWith("http://") && !url.startsWith("https://"))
//其他自定義的scheme
) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
} catch (Exception e) { //防止crash (如果手機(jī)上沒有安裝處理某個(gè)scheme開頭的url的APP, 會(huì)導(dǎo)致crash)
return true;//沒有安裝該app時(shí),返回true,表示攔截自定義鏈接,但不跳轉(zhuǎn),避免彈出上面的錯(cuò)誤頁(yè)面
}
// 使用自己的WebView組件來響應(yīng)Url加載事件,而不是使用默認(rèn)瀏覽器器加載頁(yè)面
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
isNetError = true;
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
L.d("WV", "onPageFinished");
if (isNetError) {
errorView.setVisibility(View.VISIBLE);
} else {
rlWebViewContent.setVisibility(View.VISIBLE);
errorView.setVisibility(View.GONE);
}
isNetError = false;
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
if (title.contains("網(wǎng)頁(yè)無法打開") || title.contains(url) || title.contains("404") || title.contains("500") || title.contains("Error"))
isNetError = true;
}
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
final int progress=newProgress;
new Handler().post(new Runnable() {
@Override
public void run() {
if (mProgressBar.getVisibility() == ProgressBar.GONE) {
mProgressBar.setVisibility(ProgressBar.VISIBLE);
}
mProgressBar.setProgress(progress);
mProgressBar.postInvalidate();
if (progress == 100) {
mProgressBar.setVisibility(View.GONE);
}
}
});
}
});
repeatRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWebView.reload();
}
});
webview加載progressbar的另一種方式
progressBar = ProgressDialog.show(Main.this, "Example", "Loading...");
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i(TAG, "Processing webview url click...");
view.loadUrl(url);
return true;
}
public void onPageFinished(WebView view, String url) {
Log.i(TAG, "Finished loading URL: " +url);
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
.....
});
webview.loadUrl("xxx");
}
7.webview 隱藏放大縮小按鈕 以及適配屏幕配置
mWebView.setVerticalScrollBarEnabled(false); //垂直滾動(dòng)條不顯示
mWebView.setHorizontalScrollBarEnabled(false);//水平不顯示
WebSettings webSettings = mWebView.getSettings();
webSettings.setDisplayZoomControls(false);//隱藏webview縮放按鈕
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);//屏幕適配:設(shè)置webview推薦使用的窗口,設(shè)置為true
webSettings.setLoadWithOverviewMode(true);//設(shè)置webview加載的頁(yè)面的模式,也設(shè)置為true
webSettings.setAllowFileAccess(true);
webSettings.setSupportZoom(true);//是否支持縮放
webSettings.setBuiltInZoomControls(true);//添加對(duì)js功能的支持
8.webview與js交互
Android:你要的WebView與 JS 交互方式 都在這里了
...不斷補(bǔ)充