Android Code:
Package Com.fyfeng.testjavascript;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.util.log;import Android.webkit.webview;import Android.widget.toast;public class Mainactivity extends Actionbaractivity {private Static final String tag = MainActivity.class.getSimpleName (); WebView WebView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); WebView = (WebView) This.findviewbyid (R.id.webview); Webview.getsettings (). Setjavascriptenabled (TRUE);//settings can be Javascriptwebview.loadurl ("http://192.168.1.104:8080/examples/");// Load Web page webview.addjavascriptinterface (new Payya (), "pay");//define a payment result, simulate. The Payya handles the class for the payment interface. }public void Onclickpay () {Intent Intent = new Intent (this, secondactivity.class);//Impersonation opens another payment interface. Startactivityforresult (Intent, 1000);} public void Onpaycallback (String result) {LOG.D (tag, "result =" + result); Webview.loaduRL ("Javascript:paycallback" + result + "'); /Notify Web page of payment results. } @Overridepublic void Onactivityresult (int requestcode, int resultcode, Intent data) {//Payment result returned LOG.D (tag, "Requestcode =" + Requestcode + ", ResultCode =" + ResultCode); switch (requestcode) {case: {if (ACTIVITY.RESULT_OK = = ResultCode) { Onpaycallback ("Hello, this is the payment result.!"); }break;}}} Public final class Payya {@android. webkit.javascriptinterfacepublic void Pay (String hello) {//Payment request handler function LOG.D (tag, "paid =" + Hello); Onclickpay ();} public void Show (String args) {//This function is intended to assist in displaying the Alert dialog box in the Web page. Toast.maketext (Getapplicationcontext (), args, Toast.length_short). Show ();}}
Page Part code:
<! DOCTYPE html>
Interaction between Java and JavaScript in Android (GO)