Поймать событие закрытия браузера в браузере Android - PullRequest
1 голос
/ 09 ноября 2011

Как мне поймать браузер закрыть или, более конкретно, событие выгрузки страницы в браузере Android? Я не мог заставить его работать в браузере Android с такими событиями, как unload, beforeunload и т. Д.

1 Ответ

2 голосов
/ 04 декабря 2011
    /* 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){

        }

от http://developer.android.com/reference/android/webkit/WebChromeClient.html

Я думаю, вы также можете внедрить код в веб-страницу, отображаемую WebView:

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

с использованием

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