Android:在WebView中单击会导致回调到Android应用程序 [英] Android: click within WebView causes callback to Android application

查看:68
本文介绍了Android:在WebView中单击会导致回调到Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑为我们的Android应用程序实现HTML欢迎面板,该面板会在启动屏幕上显示新闻和报价。我的问题是,现在,如果我向某个特定地点提供要约(带有 id字符串),可以触发从WebView(也许通过Java Script)到Android的回调应用程序并传递该 id字符串以启动新的 Activity ,该活动会加载并显示服务器中的数据(JSON)取决于那个 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?

第二部分已经实现并可以工作了。我主要关心的是如何在用户单击时从HTML WebView将 id字符串从HTML WebView返回到Android应用。

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.

我们更喜欢将WebView用于该特定的欢迎面板,因为它使我们可以更灵活地使用HTML进行自定义。

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

推荐答案

是的,先生,的确可以!如果您在javascript Alert中显示数据,则可以像这样捕获数据。可能不是最整洁的方法,但是它可以工作=)

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

这篇关于Android:在WebView中单击会导致回调到Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆