在Android系統(tǒng)中使用WebViewClient處理跳轉(zhuǎn)URL的方法
前言
最近代碼里和WebView有很多的交互,webview是android中的瀏覽器控件,這里主要介紹一下webview如何重載WebViewClient類來控制URL加載。
使用WebViewClient
使用WebViewClinet主要是繼承WebViewClient父類,根據(jù)需要重寫其中的方法,并在WebView中進(jìn)行配置,示例代碼如下:
webView = (WebView) findViewById(R.id.webview);
webView.setWebViewClient(new ExampleWebViewClient());
private class ExampleWebViewClient extends WebViewClient {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}
}
WebViewClient方法
1. shouldOverrideUrlLoading(WebView view, String url)
官方注釋:Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided,by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".
翻譯:當(dāng)一個(gè)新的url要在當(dāng)前WebView進(jìn)行加載的時(shí)候,這個(gè)方法給應(yīng)用一個(gè)機(jī)會(huì)來控制url的處理。如果WebView沒有setWebViewClient,則默認(rèn)操作是WebView將詢問Activity Manager獲取合適的handler處理url。如果WebView設(shè)置了setWebViewClient,返回true代表當(dāng)前應(yīng)用來處理url,返回false則代表當(dāng)前webview來處理url。如果http請(qǐng)求是POST方法,該方法將不會(huì)被調(diào)用。
代碼示例:
/**
* 所有以www.example.com開頭的url調(diào)用系統(tǒng)瀏覽器打開 其他的url在當(dāng)前webview打開
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.indexOf("http://www.example.com") != -1) {
// 調(diào)用系統(tǒng)默認(rèn)瀏覽器處理url
view.stopLoading();
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
return false;
}
2. shouleOverrideKeyEvent(WebView view, KeyEvent event)
官方注釋:Give the host application a chance to handle the key event synchronously. e.g. menu shortcut key events need to be filtered this way. If return true, WebView will not handle the key event. If return false, WebView will always handle the key event, so none of the super in the view chain will see the key event. The default behavior returns false.
翻譯:給當(dāng)前應(yīng)用一個(gè)機(jī)會(huì)來異步處理按鍵事件。返回true,WebView將不會(huì)處理該按鍵事件,返回false,WebView將處理該按鍵事件。默認(rèn)返回是false。
3. onPageStarted(WebView view, String url, Bitmap favicon)和onPageFinished(WebView view, String url)
官方注釋:Notify the host application that a page has started loading. This method is called once for each main frame load so a page with iframes or framesets will call onPageStarted one time for the main frame. This also means that onPageStarted will not be called when the contents of an embedded frame changes, i.e. clicking a link whose target is an iframe.
翻譯:當(dāng)頁面開始加載時(shí)被調(diào)用。但是,當(dāng)頁面被嵌套時(shí)(例如iframe里有一個(gè)鏈接跳轉(zhuǎn)),該方法將不會(huì)被調(diào)用。(今天就遇到了這種情況,可以通過重載onLoadResource來控制url跳轉(zhuǎn))
官方注釋:Notify the host application that a page has finished loading. This method is called only for main frame. When onPageFinished() is called, the rendering picture may not be updated yet. To get the notification for the new Picture, use onNewPicture(WebView, Picture).
翻譯:在頁面加載結(jié)束時(shí)被調(diào)用。
代碼示例:
// 獲取頁面加載時(shí)間
private long startTime;
private long endTime;
private long spendTime;
@Override
public void onPageFinished(WebView view, String url) {
endTime = System.currentTimeMillis();
spendTime = endTime - startTime;
Toast.makeText(view.getContext(), "spend time is:" + spendTime, Toast.LENGTH_SHORT).show();
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
startTime = System.currentTimeMillis();
}
4. onLoadResource(WebView view, String url)
官方注釋:Notify the host application that the WebView will load the resource specified by the given url.
翻譯:通知應(yīng)用程序WebView將要加載指定url的資源,每一個(gè)資源(例如圖片,嵌套u(yù)rl,js,css文件)。(可以通過該方法處理iframe嵌套的url)
代碼示例:
@Override
public void onLoadResource(WebView view, String url) {
if (url.indexOf("http://www.example.com") != -1 && view != null) {
view.stopLoading();
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
}
相關(guān)文章
XML Web 服務(wù) Eclipse實(shí)現(xiàn)sun-jaxws.xml文件的方法
在sun-jaxws.xml文件,可以配置endpoint、handler-chain等內(nèi)容,在這個(gè)文件中配置的內(nèi)容會(huì)覆蓋在Java代碼中使用注解屬性配置的的內(nèi)容,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2023-11-11
SpringBoot @ExceptionHandler與@ControllerAdvice異常處理詳解
在Spring Boot應(yīng)用的開發(fā)中,不管是對(duì)底層數(shù)據(jù)庫操作,對(duì)業(yè)務(wù)層操作,還是對(duì)控制層操作,都會(huì)不可避免的遇到各種可預(yù)知的,不可預(yù)知的異常需要處理,如果每個(gè)處理過程都單獨(dú)處理異常,那么系統(tǒng)的代碼耦合度會(huì)很高,工作量大且不好統(tǒng)一,以后維護(hù)的工作量也很大2022-10-10
Java實(shí)戰(zhàn)項(xiàng)目 健身管理系統(tǒng)
本文是一個(gè)Java語言編寫的實(shí)戰(zhàn)項(xiàng)目,是一個(gè)健身管理系統(tǒng),主要用到了ssm+springboot等技術(shù),技術(shù)含量筆記高,感興趣的童鞋跟著小編往下看吧2021-09-09
spring-security關(guān)于hasRole的坑及解決
這篇文章主要介紹了spring-security關(guān)于hasRole的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
詳細(xì)聊聊Spring MVC重定向與轉(zhuǎn)發(fā)
大家應(yīng)該都知道請(qǐng)求重定向和請(qǐng)求轉(zhuǎn)發(fā)都是web開發(fā)中資源跳轉(zhuǎn)的方式,這篇文章主要給大家介紹了關(guān)于Spring MVC重定向與轉(zhuǎn)發(fā)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09
避免多個(gè)jar通過maven打包導(dǎo)致同名配置文件覆蓋沖突問題
這篇文章主要介紹了避免多個(gè)jar通過maven打包導(dǎo)致同名配置文件覆蓋沖突問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Java代碼中與Lua相互調(diào)用實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

