kotlin如何使用新的webView打开javascript的window.open

时间:2019-11-05 05:41:29

标签: android kotlin android-webview

我引用this,遇到window.open时想打开一个新的webView。我尝试使用硬代码打开Goog​​le页面,这是我的代码

class MainActivity : AppCompatActivity() {

    private var myWebView: WebView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        myWebView = findViewById(R.id.webview)
        myWebView!!.settings.javaScriptEnabled = true
        myWebView!!.settings.domStorageEnabled = true
        myWebView!!.settings.setSupportMultipleWindows(true)
        myWebView!!.settings.javaScriptCanOpenWindowsAutomatically = true

        myWebView!!.webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                view?.loadUrl(url)
                return true
            }
        }

        myWebView!!.webChromeClient = object : WebChromeClient() {
            ....

            override fun onCreateWindow(
                view: WebView,
                dialog: Boolean,
                userGesture: Boolean,
                resultMsg: android.os.Message
            ): Boolean {
                var cookieValues: HashMap<String, String> = _parseCookie(view.url)
                var tokenData: HashMap<String, String> = HashMap()
                val newWebView = WebView(view.context)
                view.addView(newWebView)

                newWebView.settings.javaScriptEnabled = true
                newWebView.settings.setSupportZoom(true)
                newWebView.settings.builtInZoomControls = true
                newWebView.settings.setSupportMultipleWindows(true)

                val transport = resultMsg.obj as WebView.WebViewTransport
                transport.webView = newWebView
                resultMsg.sendToTarget()

                newWebView.webViewClient = object : WebViewClient(){
                    override fun shouldOverrideUrlLoading(
                        view: WebView, url: String
                    ): Boolean {
                        view.loadUrl("https://www.google.com")
                        return true
                    }
                }

                return true    
            }
        }

        myWebView!!.loadUrl("http://www.example.com")
    }

    .... 
}

这是可行的,但是新的webView大小没有全屏显示,然后单击搜索输入,此应用程序关闭,我收到了此消息。

E/EGL_emulation: tid 8491: eglSurfaceAttrib(1354): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xd623bbc0, error=EGL_BAD_MATCH
W/cr_AwAutofillManager: Application attempted to call on a destroyed AwAutofillManager
    java.lang.Throwable
        at org.chromium.android_webview.AwAutofillManager.checkAndWarnIfDestroyed(AwAutofillManager.java:18)
        at org.chromium.android_webview.AwAutofillProvider.startAutofillSession(AwAutofillProvider.java:98)
        at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
        at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:9)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6680)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
W/cr_AwAutofillManager: Application attempted to call on a destroyed AwAutofillManager
    java.lang.Throwable
        at org.chromium.android_webview.AwAutofillManager.checkAndWarnIfDestroyed(AwAutofillManager.java:18)
        at org.chromium.android_webview.AwAutofillManager.notifyVirtualViewEntered(AwAutofillManager.java:11)
        at org.chromium.android_webview.AwAutofillProvider.startAutofillSession(AwAutofillProvider.java:103)
        at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
        at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:9)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6680)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

我该如何解决?谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这对我来说很有效,我需要将layoutParams设置为新的webView。所以我重用了原始视图布局。

newWebView.layoutParams = view.layoutParams