在Android开发中,使用WebView控件,可以很方便的实现Web浏览器的功能。
WebView官方详细文档: http://developer.android.com/reference/android/webkit/WebView.html
使用WebView加载一个网页很简单:
WebView webView = new WebView(this);
webView.loadUrl("http://www.google.com/");
webView.loadUrl("file:///android_asset/test.html");
webView.loadUrl("content://com.android.htmlfileprovider/sdcard/test.html");
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setSupportMultipleWindows(true);
如果要监听WebView的各种通知,请求。可以使用一个自定义的WebViewClient:
public class MyWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
@Override
public void doUpdateVisitedHistory(WebView view, String url,
boolean isReload)
{
// TODO Auto-generated method stub
super.doUpdateVisitedHistory(view, url, isReload);
}
@Override
public void onFormResubmission(WebView view, Message dontResend,
Message resend)
{
// TODO Auto-generated method stub
super.onFormResubmission(view, dontResend, resend);
}
@Override
public void onLoadResource(WebView view, String url)
{
// TODO Auto-generated method stub
super.onLoadResource(view, url);
}
@Override
public void onPageFinished(WebView view, String url)
{
// TODO Auto-generated method stub
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl)
{
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
@Override
public void onReceivedHttpAuthRequest(WebView view,
HttpAuthHandler handler, String host, String realm)
{
// TODO Auto-generated method stub
super.onReceivedHttpAuthRequest(view, handler, host, realm);
}
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error)
{
// TODO Auto-generated method stub
super.onReceivedSslError(view, handler, error);
}
@Override
public void onScaleChanged(WebView view, float oldScale, float newScale)
{
// TODO Auto-generated method stub
super.onScaleChanged(view, oldScale, newScale);
}
@Override
public void onTooManyRedirects(WebView view, Message cancelMsg,
Message continueMsg)
{
// TODO Auto-generated method stub
super.onTooManyRedirects(view, cancelMsg, continueMsg);
}
@Override
public void onUnhandledKeyEvent(WebView view, KeyEvent event)
{
// TODO Auto-generated method stub
super.onUnhandledKeyEvent(view, event);
}
@Override
public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event)
{
// TODO Auto-generated method stub
return super.shouldOverrideKeyEvent(view, event);
}
}
然后将其设置到WebView中:
MyWebViewClient webViewClient=new MyWebViewClient();
webView.setWebViewClient(webViewClient);
public class MyWebChromeClient extends WebChromeClient
{
@Override
public void onProgressChanged(WebView view, int newProgress)
{
if(newProgress==100)
{
view.setVisibility(View.VISIBLE);
}
}
@Override
public Bitmap getDefaultVideoPoster()
{
// TODO Auto-generated method stub
return super.getDefaultVideoPoster();
}
@Override
public View getVideoLoadingProgressView()
{
// TODO Auto-generated method stub
return super.getVideoLoadingProgressView();
}
@Override
public void getVisitedHistory(ValueCallback callback)
{
// TODO Auto-generated method stub
super.getVisitedHistory(callback);
}
@Override
public void onCloseWindow(WebView window)
{
// TODO Auto-generated method stub
super.onCloseWindow(window);
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage)
{
// TODO Auto-generated method stub
return super.onConsoleMessage(consoleMessage);
}
@Override
public void onConsoleMessage(String message, int lineNumber,
String sourceID)
{
// TODO Auto-generated method stub
super.onConsoleMessage(message, lineNumber, sourceID);
}
@Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg)
{
// TODO Auto-generated method stub
return super.onCreateWindow(view, dialog, userGesture, resultMsg);
}
@Override
public void onExceededDatabaseQuota(String url,
String databaseIdentifier, long currentQuota,
long estimatedSize, long totalUsedQuota,
QuotaUpdater quotaUpdater)
{
// TODO Auto-generated method stub
super.onExceededDatabaseQuota(url, databaseIdentifier, currentQuota,
estimatedSize, totalUsedQuota, quotaUpdater);
}
@Override
public void onGeolocationPermissionsHidePrompt()
{
// TODO Auto-generated method stub
super.onGeolocationPermissionsHidePrompt();
}
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
Callback callback)
{
// TODO Auto-generated method stub
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
@Override
public void onHideCustomView()
{
// TODO Auto-generated method stub
super.onHideCustomView();
}
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result)
{
// TODO Auto-generated method stub
return super.onJsAlert(view, url, message, result);
}
@Override
public boolean onJsBeforeUnload(WebView view, String url,
String message, JsResult result)
{
// TODO Auto-generated method stub
return super.onJsBeforeUnload(view, url, message, result);
}
@Override
public boolean onJsConfirm(WebView view, String url, String message,
JsResult result)
{
// TODO Auto-generated method stub
return super.onJsConfirm(view, url, message, result);
}
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result)
{
// TODO Auto-generated method stub
return super.onJsPrompt(view, url, message, defaultValue, result);
}
@Override
public boolean onJsTimeout()
{
// TODO Auto-generated method stub
return super.onJsTimeout();
}
@Override
public void onReachedMaxAppCacheSize(long spaceNeeded,
long totalUsedQuota, QuotaUpdater quotaUpdater)
{
// TODO Auto-generated method stub
super.onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, quotaUpdater);
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon)
{
// TODO Auto-generated method stub
super.onReceivedIcon(view, icon);
}
@Override
public void onReceivedTitle(WebView view, String title)
{
// TODO Auto-generated method stub
super.onReceivedTitle(view, title);
}
@Override
public void onReceivedTouchIconUrl(WebView view, String url,
boolean precomposed)
{
// TODO Auto-generated method stub
super.onReceivedTouchIconUrl(view, url, precomposed);
}
@Override
public void onRequestFocus(WebView view)
{
// TODO Auto-generated method stub
super.onRequestFocus(view);
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback)
{
// TODO Auto-generated method stub
super.onShowCustomView(view, callback);
}
}
然后把自定义的client注册到WebView中去:
MyWebChromeClient webChromeClient=new MyWebChromeClient();
webView.setWebChromeClient(webChromeClient);