Android Activity 到 WebView 和 WebView 到 Activity 参数传递 [英] Android Activity to WebView and WebView to Activity Parameter Passing
问题描述
我有一个应用程序,其中的活动与我们的服务器通信并获取未结金额的详细信息.我希望用户使用支付网关支付未付金额.对于支付网关,我必须从 webview 调用网页并将未结金额详细信息和其他凭据从活动传输到 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.
这是我想要在活动和 webview 中加载的页面之间实现的双向通信.我试图找到一种方法来做到这一点,但找不到一个简单的例子来进行这种交换.
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.
主要概念是,您在包含 WebView 的 Activity 中创建一个 javascript 界面.
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.
}
}
将界面添加到您的 webView
Add the interface to your webView
webView.addJavascriptInterface(new WebPayInterface(), "WebPayInterface");
最后在使用 javascript 的 html 代码中,您可以调用
Finally in your html code using javascript you can call
WebPayInterface.PaymentFinished(100, true);
这篇关于Android Activity 到 WebView 和 WebView 到 Activity 参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!