-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaymentWebView.java
More file actions
46 lines (38 loc) · 1.42 KB
/
PaymentWebView.java
File metadata and controls
46 lines (38 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.app.paymentapp.PaymentLibrary;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class PaymentWebView {
URLChangedCallback callback;
WebView webView;
// Set the content/Url on the webView.
public void setPaymentWebView(WebView webView, String URL, final URLChangedCallback callback) {
this.callback = callback;
this.webView = webView;
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(mWebViewClient);
webView.addJavascriptInterface(new MyJavaScriptInterface(),
"android");
webView.loadUrl(URL);
}
WebViewClient mWebViewClient = new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:window.android.onUrlChange(window.location.href);");
}
};
// This method trigger every time whenever the URL change.
class MyJavaScriptInterface {
@JavascriptInterface
public void onUrlChange(String url) {
Log.e("hydrated", "onUrlChange" + url);
// Close webView or perform any action
callback.onAction(url);
}
}
// Trigger when url changed
public interface URLChangedCallback {
void onAction(String data);
}
}