Android and H5 intermodulation steps

Source: Internet
Author: User

1. Call JS in Android

If: H5 page has a section of the following JS code

function H5test (str) {xxxx ...}

Android calls in the following ways:

Step one: Start support JS

WebSettings ws = GetSettings ();

Ws.setjavascriptenable (True);

Step Two: Load the Web page

Webview.loadurl (URL);

Step three: Call JS in the Web page, note that the function name in the above JS code in the name of the letter exactly the same

Webview.loadurl ("Javascript:changetitle (' Android call JS ')");

2.JS Call Android

Suppose you click a button on the H5 page to jump to an activity page in your Android code

Step one: Start support JS

WebSettings ws = GetSettings ();

Ws.setjavascriptenabled (TRUE);

Step two: Add the JS interface class to WebView, which encapsulates the native operation. Parameter 2 is the entity class name in JS, which needs to be consistent with the name in JS code.

Webview.addjavascriptinterface (New Jsinterface (), "js2android");

H5 page See js2android will go to the Jsinterface () class to find the appropriate method.

Step three: Write the contents of the interface class

public class jsinterface{@JavascriptInterface  //This note must be taken with public void Selectpic () {Intent Intent = new Intent (We    Bviewactivity.this,getpicactivity.class);  Startactivityforresult (intent,100); }}

Step four: H5 page js corresponding code

function Appselectpic () {javascript:js2android.selectPic ();}

Through the above 4 steps, you can click on the H5 page of a button to jump to the corresponding activity

Android and H5 intermodulation steps

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.