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

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

问题描述

我正在考虑为我们的 Android 应用实现一个 HTML 欢迎面板,它在启动屏幕上显示新闻和优惠.我现在的问题是,如果我向特定位置(带有 id 字符串)提供报价,我是否可以触发从 WebView(可能通过 Java Script)到 Android 应用程序的回调并传递该 id string 让它开始一个新的 Activity,它根据 id string 从服务器 (JSON) 加载和显示数据?

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 string 返回给 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天全站免登陆