Android: click within WebView causes callback to A

2019-02-19 10:07发布

I'm thinking of implementing a HTML welcome panel to our Android app, which presents news and offers on the start-up screen. My question is now, if I present an offer to a specific place (with an id string) can I trigger a callback from the WebView (maybe via Java Script) to the Android app and passing that id string to make it start a new Activity which loads and shows data from a server (JSON) depending on that id string?

The second part is already implemented and working. My main concern is how to get the id string from the HTML WebView back to the Android app when the user clicks on it.

We prefer to use a WebView for that specific welcome panel, because it gives us more flexibility to customize by using HTML.

2条回答
▲ chillily
2楼-- · 2019-02-19 10:31

yes indeed you can good sir! If you show the data in a javascript Alert, then you can capture it like this. It might not be the most neat way, but it works =)

private void loadWebViewStuff() {
        myWebView.setWebChromeClient(new MyWebChromeClient());
        myWebView.loadUrl(URL);
}


final class MyWebChromeClient extends WebChromeClient {
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        //"message" is what is shown in the alert, here we can do whatever with it
    }
}
查看更多
劳资没心,怎么记你
3楼-- · 2019-02-19 10:32

It's probably better to use WebView.addJavascriptInterface, rather than overload onJsAlert.

查看更多
登录 后发表回答