1
所以我想能夠監視我的WebView應用程序加載的URL。我試圖完成的主要事情是將WebView保存在特定網站中,或者至少能夠檢測用戶何時離開網站的URL。如何「監視」WebView中的URL基址
例子。如果我擁有www.google.com的基本網址,並且當用戶點擊圖片時,他會得到「http://www.google.com/imghp」但是它仍然具有www.google.com的基本網址,因此如果有方法可以監控基本網址不再是www.google.com,例如用戶點擊搜索結果,然後完全轉到其他網站。
這樣做的目的是,我想向用戶顯示一個對話框或Toast,讓他們知道他們將離開WebView應用程序創建的網站。
任何代碼,想法或建議將不勝感激。
示例代碼
@Override
public void onLoadResource(WebView view, String url)
{
if (url.equals("http://www.google.com"))
{
//do your own thing here
view.loadUrl(url);
}
else
{
AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(Webview_Main.this);
localBuilder2.setTitle("Hey Wait!");
localBuilder2.setMessage("You currently are about to leave the website\n\nIf you want to stay inside this website, Please click the Go Back button below");
localBuilder2.setIcon(R.drawable.ic_launcher);
localBuilder2.setPositiveButton("Go Back",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface paramDialogInterface,
int paramInt) {
web.goBack();
}
});
localBuilder2.setNegativeButton("Continue",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface paramDialogInterface,
int paramInt) {
}
});
localBuilder2.show();
};
super.onLoadResource(view, url);
}
然而,這上面的代碼不工作,只保留坡平了,因爲URL可能不同的警告對話框。
搜索['WebViewClient#shouldOverrideUrlLoading(...)'](http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldOverrideUrlLoading%28android.webkit.WebView ,%20java.lang.String%29)和['WebViewClient#shouldInterceptRequest(...)'](http://developer.android.com/reference/android/webkit/WebViewClient.html#shouldInterceptRequest%28android.webkit。 WebView中,%20java.lang.String%29)。關於如何攔截將要加載的網址的大量示例。 – 2013-04-10 23:03:22
[WebView - 點擊URL打開默認瀏覽器]的可能重複(http://stackoverflow.com/questions/2378800/webview-clicking-urls-opens-default-browser) – 2013-04-10 23:04:47
@MH。感謝評論,我已經添加了一些代碼,你可以看看。 – 2013-04-11 16:07:01