Android的活动来的WebView和的WebView到活动参数传递参数、Android、WebView

2023-09-12 22:12:49 作者:人生若只如初见

我在其中的活动与我们的服务器进行通信,并获取余额信息的应用程序。我希望用户使用支付网关支付欠款。对于支付网关,我需要从web视图调用Web页,并从活动转移余额详情及其他凭证到的WebView加载页面。在交易结束时,我再一次需要得到一些参数的网页回来的WebView到活动。

I have an application in which an activity communicates with our server and gets the outstanding amount details. I want users to pay the outstanding amount using a payment gateway. for Payment Gateway, I have to call web page from webview and transfer outstanding amount details and other credentials from activity to the page loaded in webview. At the end of transaction I once again need to get some parameters back from webpage in webview to activity.

这是我想要的活动,并在web视图加载的页面之间实现了双向通信。我试图找出一种方法来做到这一点,但无法找到一个简单的例子,做这样的交流。

This is a two way communication which I want to achieve between an activity and a page loaded in webview. I tried to find out a way to do it but could not find a simple example which does such exchange.

推荐答案

您可以实现你想要使用一个JavaScript WebAppInterface什么作为证明的此处。

You could achieve what you want using a javascript WebAppInterface as demonstrated here.

主要的概念是,在创建活动内的JavaScript接口牵着你的WebView。

The main concept is that, you create a javascript interface inside the Activity holding your WebView.

private class WebPayInterface {
    int amount;
    boolean success;

    @JavascriptInterface
    public void PaymentFinished(int amount, boolean success) {
        this.amount = amount;
        this.success = success;

        // do whatever you want in the parent activity.
    }
}

将接口加入到你的web视图

Add the interface to your webView

webView.addJavascriptInterface(new WebPayInterface(), "WebPayInterface");

最后,在你的HTML code使用JavaScript就可以调用

Finally in your html code using javascript you can call

WebPayInterface.PaymentFinished(100, true);