一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 在Android系統中使用WebViewClient處理跳轉URL的方法

在Android系統中使用WebViewClient處理跳轉URL的方法

2019-12-31 14:43低調小一 JAVA教程

這篇文章主要介紹了在Android系統中使用WebViewClient處理跳轉URL的方法,實現代碼為Java語言編寫,是需要的朋友可以參考下

前言
最近代碼里和WebView有很多的交互,webview是android中的瀏覽器控件,這里主要介紹一下webview如何重載WebViewClient類來控制URL加載。

使用WebViewClient
使用WebViewClinet主要是繼承WebViewClient父類,根據需要重寫其中的方法,并在WebView中進行配置,示例代碼如下:

   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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". 

翻譯:當一個新的url要在當前WebView進行加載的時候,這個方法給應用一個機會來控制url的處理。如果WebView沒有setWebViewClient,則默認操作是WebView將詢問Activity Manager獲取合適的handler處理url。如果WebView設置了setWebViewClient,返回true代表當前應用來處理url,返回false則代表當前webview來處理url。如果http請求是POST方法,該方法將不會被調用。
代碼示例:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
 * 所有以www.example.com開頭的url調用系統瀏覽器打開 其他的url在當前webview打開
 */
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
  if (url.indexOf("http://www.example.com") != -1) {
    // 調用系統默認瀏覽器處理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. 

翻譯:給當前應用一個機會來異步處理按鍵事件。返回true,WebView將不會處理該按鍵事件,返回false,WebView將處理該按鍵事件。默認返回是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. 

翻譯:當頁面開始加載時被調用。但是,當頁面被嵌套時(例如iframe里有一個鏈接跳轉),該方法將不會被調用。(今天就遇到了這種情況,可以通過重載onLoadResource來控制url跳轉)

    官方注釋: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). 

翻譯:在頁面加載結束時被調用。
代碼示例:

    // 獲取頁面加載時間  
   

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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. 

翻譯:通知應用程序WebView將要加載指定url的資源,每一個資源(例如圖片,嵌套url,js,css文件)。(可以通過該方法處理iframe嵌套的url)
代碼示例:

?
1
2
3
4
5
6
7
@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)));
  }      
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲电影成人 成人影院 | 国产成人综合网亚洲欧美在线 | 双性太子| 国产成人久久精品区一区二区 | 国产精品女主播自在线拍 | 操碰人人 | 香蕉视频在线观看网址 | 精品久久久久香蕉网 | 韩国日本在线观看 | 免费观看一级一片 | porno movie hd高清 | 免费高清视频免费观看 | 成年无限观看onlyfans | 美女跪式抽搐gif动态图 | 激情综合色啪啪小说 | 午夜福利在线观看6080 | 高清在线观看mv的网址免费 | 精品国产一区二区在线观看 | 国产在线欧美日韩精品一区二区 | bt伙计最新合集 | 亚洲国产福利精品一区二区 | 国产一区二区在线观看视频 | 免费观看a毛片一区二区不卡 | 国产精品青青青高清在线 | 成人免费视频大全 | 亚洲一区二区三区不卡在线播放 | 美女在线看永久免费网址 | 欧美一区二区三区免费观看视频 | 出差被灌醉绝伦的上司日本 | 亚洲精品国偷拍自产在线观看蜜臀 | 91综合久久 | 四虎影院大全 | 2019国产精品| 日本高清在线播放 | 韩日视频在线观看 | 免费标准高清看机机桶机机 | 精品日韩欧美一区二区三区在线播放 | 国产卡一卡二卡3卡乱码免费 | 狠狠色成人综合 | 日韩专区| 欧美va在线观看 |