开发者

Android: click within WebView causes callback to Android application

开发者 https://www.devze.com 2022-12-19 14:15 出处:网络
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

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 开发者_JAVA技巧panel, because it gives us more flexibility to customize by using HTML.


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


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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号