WebView a button in the application to invoke the JS event of an HTML page

Source: Internet
Author: User

public class Methodmutual extends Activity {

Private WebView Mwebview;
Private Handler Mhandler = new Handler ();
private static final String Log_tag = "Webviewdemo";

/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Loadassethtml ();
}

public void loadassethtml () {
Mwebview = (WebView) Findviewbyid (R.id.webview);
WebSettings websettings = Mwebview.getsettings ();
Websettings.setsavepassword (FALSE);
Websettings.setsaveformdata (FALSE);
Websettings.setjavascriptenabled (TRUE);
Websettings.setsupportzoom (FALSE);
Mwebview.setwebchromeclient (New Mywebchromeclient ());
Binds a Java object to a JavaScript object, the JavaScript object name is InterfaceName, and the scope is global.
Mwebview.addjavascriptinterface (New Demojavascriptinterface (), "demo");
Mwebview.loadurl ("file:///android_asset/demo.html");
Trigger the JS function response by clicking the button in the app
Button Mcalljs = (button) Findviewbyid (R.ID.MCALLJS);
Mcalljs.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Mwebview.loadurl ("Javascript:wave ()");
}
});
}

private int i = 0;

Final class Demojavascriptinterface {
Demojavascriptinterface () {
}
/**
* This isn't called on the UI thread. Post a runnable to invoke
* Loadurl on the UI thread.
*/
public void Callandroid () {
Mhandler.post (New Runnable () {
public void Run () {
if (i% 2 = = 0) {
Mwebview.loadurl ("Javascript:wave ()");
} else {
Mwebview.loadurl ("Javascript:waveback ()");
}
i++;
}
});
}
}

/**
* Provides a hook for calling ' alert ' from JavaScript. Useful for debugging
* Your JavaScript.
*/
Final class Mywebchromeclient extends Webchromeclient {
@Override
public boolean Onjsalert (WebView view, string URL, String message,
Jsresult result) {
LOG.D (log_tag, message);
Result.confirm ();
return true;
}
}
}

WebView a button in the application to invoke the JS event of an HTML page

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.