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
}
}
精彩评论