1.首先 重寫 onKeyDown方法 添加如下代碼:
/**
* 按鍵響應,在WebView中檢視網頁時,檢查是否有可以前進的曆史記錄。
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
// 傳回鍵退回
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up
// to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
2.代碼寫到這裡會出現一個問題,多次點選底部導航按鈕 再點選傳回鍵 會相應WebView的GoBack,導緻在首頁 底部導航反向跳而不退出應用
解決辦法,
a. 聲明成員變量 :
private boolean isMainAvtivity = true;
b.onKeyDown方法中 改寫成如下 意思是如果
isMainAvtivity是true的話 出走退出應用的代碼 如果是false的話 相應webview的GoBack事件
/**
* 按鍵響應,在WebView中檢視網頁時,檢查是否有可以前進的曆史記錄。
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if (isMainAvtivity) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程式", Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
}
return true;
}
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
// 傳回鍵退回
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up
// to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
3.
isMainAvtivity 的值 是通過過濾網頁進行true或者false的指派
代碼如下:
// 覆寫WebView預設使用第三方或系統預設浏覽器打開網頁的行為,使網頁用WebView打開
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!TextUtils.isEmpty(url)) {
videoFlag = url.contains("vedio");
}
if (url.trim().startsWith("tel")) {//特殊情況tel,調用系統的撥号軟體撥号【<a href="tel:1111111111" target="_blank" rel="external nofollow" >1111111111</a>】
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
} else {
String port = url.substring(url.lastIndexOf(":") + 1, url.lastIndexOf("/"));//嘗試要攔截的視訊通訊url格式(808端口):【http://xxxx:808/?roomName】
if (port.equals("808")) {//特殊情況【若打開的連結是視訊通訊位址格式則調用系統浏覽器打開】
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
} else {//其它非特殊情況全部放行
view.loadUrl(url);
}
}
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.e("========", "66666 ");
//重新onPageStarted方法 如果加載的是一級頁面 isMainActivity==true,
if ("http://..../index.php?a=/home/user/shouye.html".equals(url)) {
isMainAvtivity = true;
}
if ("http://...../index.php?a=/home/shop/index.html".equals(url)) {
isMainAvtivity = true;
}
if ("http://...../index.php?a=/home/user/zhuanpan.html".equals(url)) {
isMainAvtivity = true;
}
if ("http://...../index.php?a=/home/user/index.html".equals(url)) {
isMainAvtivity = true;
}
//如果是從一級頁面跳轉到二級的頁面 通通給于false,可以用contains進行判斷
if (url != null) {
if (url.contains("http://...../index.php?a=/home/shop/xiangqing/id/")) {
isMainAvtivity = false;
}
}
if (url != null) {
if (url.contains("http://...../index.php?a=/home/shop/jiesuan/id/")) {
isMainAvtivity = false;
}
}
if ("http://...../index.php?a=/home/user/shengji.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/updatemembermsg.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/zjgl.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/dingdan.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/userlist.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/index/index.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/cz_record.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/chongzhi.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/tixian.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/tixian_record.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/bonus/financialflow.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/gonggao/param/1.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/gonggao/param/2.html".equals(url)) {
isMainAvtivity = false;
}
if ("http://...../index.php?a=/home/user/chongzhi".equals(url)) {
isMainAvtivity = false;
}
}
@Override
public void onPageFinished(WebView view, String url) {
//頁面加載完畢,結束對話框
}
@Override
public void onLoadResource(WebView view, String url) {
//加載頁面資源,比如每加載一張圖檔就會調用
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//頁面加載錯誤回掉
switch (errorCode) {
case 404:
// view.loadUrl("file:///android_assets/error_handle.html");
break;
}
}
});
3.至此 大功告成