自定義的webview
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* 自定義的瀏覽器
*
*/
public class UIWebViewClient extends WebViewClient {
/***跳轉上下文**/
private Context context;
/***跳轉界面對象**/
private Class target;
/***是否程序內部打開:true-->程序內部打開網頁,false-->調用別的窗體打開**/
private Boolean isInnerOpen = true;
/**過濾目標字符串**/
private String filter = "";
private String TAG = UIWebViewClient.class.getSimpleName();
/**當前加載網頁URL***/
public static String currentURL = "";
/**
* 默認內部打開鏈接
*/
public UIWebViewClient() {
this(true);
}
/**
* @param isInnerOpen 是否內部打開
*/
public UIWebViewClient(Boolean isInnerOpen) {
this(null,null,isInnerOpen);
}
/**
*
* @param context 上下文
* @param target 處理新開網頁URL界面
* @param isInnerOpen 是否內部打開
*/
public UIWebViewClient(Context context, Class target, Boolean isInnerOpen) {
this(context,target,isInnerOpen,"");
}
/**
*
* @param context 上下文
* @param target 處理新開網頁URL界面
* @param isInnerOpen 是否內部打開
* @param filter 過濾字符串
*/
public UIWebViewClient(Context context, Class target, Boolean isInnerOpen,String filter) {
this.context = context;
this.target = target;
this.isInnerOpen = isInnerOpen;
this.filter = filter;
}
/***
* 讓瀏覽器支持訪問https請求
*/
@SuppressLint("NewApi")
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
handler.proceed();
super.onReceivedSslError(view, handler, error);
}
/**
* 控制網頁的鏈接跳轉打開方式(攔截URL)
*/
public boolean shouldOverrideUrlLoading(WebView view, String url) {
currentURL = url;
if (isInnerOpen) {
view.loadUrl(url);
return true;
} else {
String host = Uri.parse(url).getHost();
if (!host.startsWith("http")){
host = "http://" + host;
}
if (!host.endsWith("/")){
host = host + "/";
}
if (null != context && null != target) {
if (host.equals(filter)) {
Intent intent = new Intent(context, target);
intent.putExtra("url", url);
context.startActivity(intent);
return true;
}
}
return false;
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.e(TAG, "onPageStarted--->url="+url);
}
@Override
public void onPageFinished(WebView view, String url) {
//加載完畢後,開始加載圖片
//view.getSettings().setBlockNetworkImage(false);
Log.e(TAG, "onPageFinished--->url="+url);
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode,String description, String failingUrl) {
ToolAlert.closeLoading();
ToolAlert.toastShort("加載數據失敗,錯誤碼:"+errorCode+ "\n 原因描述:"+description);
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.