2011-11-09 336 views
1

如何在android瀏覽器中捕捉瀏覽器關閉或更具體的頁面卸載事件?我不能讓它在Android瀏覽器http://developer.android.com/reference/android/webkit/WebChromeClient.html在Android瀏覽器中捕捉瀏覽器關閉事件

工作與活動,如unloadbeforeunload

+0

你說話通過使用Java編程或從移動網絡來處理這個問題? –

+0

WebViewClient可能對您有所幫助:http://developer.android.com/reference/android/webkit/WebViewClient.html –

+0

即時通訊討論如何在JavaScript中處理此事件。 – Hari

回答

2
/* Tell the client to display a dialog to confirm navigation away from 
the current page. This is the result of the onbeforeunload javascript event. 
If the client returns true, WebView will assume that the client will handle 
the confirm dialog and call the appropriate JsResult method. If the client 
returns false, a default value of true will be returned to javascript to accept 
navigation away from the current page. The default behavior is to return false. 
Setting the JsResult to true will navigate away from the current page, false will 
cancel the navigation. 
    */ 
     public boolean onJsBeforeUnload (WebView view, String url, String message, JsResult result){ 

     } 

我想你可能也將代碼注入通過網頁視圖顯示在您的網頁:

<script type="text/javascript"> 
    function OnBeforeUnLoad() { 
     return ""; 
    } 
</script> 

使用

@Override 
    public void onPageFinished(WebView view, String url) 
    { 
     webview.loadUrl("string with your script"); 
    }