Interaction between Java and JavaScript in Android

Source: Internet
Author: User
Tags apache tomcat

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.