简体   繁体   English

window.close()无法在android webview中运行?

[英]window.close() not working in android webview?

Android 4.1 Eclipse Testing on device Android 4.1 Eclipse在设备上进行测试

I am trying to open a html/javascript webpage, hosted on my localhost for now, in my android application through a webview. 我试图通过webview在我的android应用程序中打开一个html / javascript网页,暂时托管在我的localhost上。 The page is suppose to just close after 2 sec through the below code 假设页面在2秒后通过以下代码关闭

<script type="text/javascript">
    alert('hello');
    setInterval(function(){window.close();},2000);
</script>

I am able to get the hello alert by using WebChromeClient. 我可以使用WebChromeClient获取hello警报。 But the window.close is still not working. 但是window.close仍然无法正常工作。 Any help would be appreciated. 任何帮助,将不胜感激。

PS: Here is how i am loading the URL: PS:这是我加载URL的方式:

WebSettings ws = webView.getSettings();
ws.setJavaScriptEnabled(true);      
ws.setJavaScriptCanOpenWindowsAutomatically(true);
webView.loadUrl("http://192.168.1.137/abc.html");
webView.setWebChromeClient(new WebChromeClient());

Try this : 试试这个 :

WebChromeClient webClient = new WebChromeClient(){

    public void onCloseWindow(Window w){
        super.onCloseWindow(w);
        Log.d(TAG, "Window close");
    }
};

Description : 说明:

public void onCloseWindow (WebView window)

Notify the host application to close the given WebView and remove it from the view system if necessary. 通知主机应用程序关闭给定的WebView,并在必要时将其从视图系统中删除。 At this point, WebCore has stopped any loading in this window and has removed any cross-scripting ability in javascript. 此时,WebCore已停止在此窗口中的任何加载,并已删除javascript中的任何跨脚本编写功能。

It's not possible on android version > KITKAT. 这在Android版> KITKAT上是不可能的。 But I have find solution. 但我找到了解决方案。 If android build version > KITKAT, I have detected message from js on server side in method onConsoleMessage of WebChromeClient class http://developer.android.com/reference/android/webkit/WebChromeClient.html . 如果机器人生成版本> KITKAT,我已经检测到的消息从WebChromeClient类的方法onConsoleMessage在服务器端的js http://developer.android.com/reference/android/webkit/WebChromeClient.html It works for me. 这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM