Skip to content Skip to sidebar Skip to footer

How To Show An Activity With Javascript Code In Webview?

My application has a first activity that contains Webview and loads a simple page from the server in the beginning of the application. Page code is:

Solution 1:

Here's what you do in your onCreate:

classMyJavaScriptInterface {
    @JavascriptInterfacepublicvoiddoStuff(String param) {

        runOnUiThread(newRunnable() {
            @Overridepublicvoidrun() {
                // start Video Activity here
            }
        });
    }
}

final WebView webView = (WebView) findViewById(R.id.browser);
webView.setWebChromeClient(newWebChromeClient());

/* enable JavaScript */
webView.getSettings().setJavaScriptEnabled(true);

/* Register a new JavaScript interface called Android */
webView.addJavascriptInterface(newMyJavaScriptInterface(), "Android");

/* load a web page */
webView.loadUrl(url);

In your loaded website, when the button is clicked, run

Android.doStuff("video1"); // calling the @JavascriptInterface method

Post a Comment for "How To Show An Activity With Javascript Code In Webview?"