Android and JS call each other and note

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size:18px;">Recent projects often use HTML5 and Android and JS calls are often used, here to record, test system more than 5.0.</span></p></p><p><p><span style="font-size:18px;">Here first paste the source code</span></p></p><p><p><span style="font-size:18px;">Activity:</span></p></p><p><p><span style="font-size:18px;"></span></p></p><pre name="code" class="java">Package Jwzhangjie.com.webviewandjs;import Android.content.context;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.webkit.javascriptinterface;import Android.webkit.webview;import Android.webkit.webviewclient;import Android.widget.toast;public class Mainactivity extends appcompatactivity {private WebView WebView; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); WebView = (webView) Findviewbyid (r.id.webview); Initwebview (); } private void Initwebview () {//set encoding webview.getsettings (). setdefaulttextencodingname ("utf-8"); Support JS webview.getsettings (). setjavascriptenabled (true); Set the Listener event webview.setwebviewclient (new jiewewviewclient ()); Sets the local calling object and its interface webview.addjavascriptinterface (new javascriptobject (getapplicationcontext ()), "toandroid"); Load JS Webview.loadurl ("file:///android_asset/index.html"); } public void Javacalljsnoparams (view view) {webview.loadurl ("javascript:javacalljsnoparamsmethod ()"); } public void Javacalljshasparams (view view) {webview.loadurl ("javascript:javacalljshasparamsmethod ('" + 123 + ") ‘)"); } public class Javascriptobject {Context mcontxt; Public Javascriptobject (Context Mcontxt) {this.mcontxt = mcontxt; } @JavascriptInterface//sdk17 version above plus annotated public void Jscalljavanoparams () {toast.maketext (mcontxt, "js calls Java method (no parameter)", toast.length_long). show (); } @JavascriptInterface//sdk17 version plus annotated public void Jscalljavahasparams (String Params) {toast.mak EText (mcontxt, "js calls Java method (with parameters):" + params, toast.length_short). show (); }} class Jiewewviewclient extends webviewclient{/** * If followed by * Webview.loadurl ("file:///an Droid_asset/index.html "); * Calling JS method is not working, must be page loading complete can */@Override public void onpagefinished (WebView view, String ur L) {super.onpagefinished (view, url); Webview.loadurl ("javascript:javacalljshasparamsmethod ('" + 123 + "')"); } }}</pre><br>Html:<p><p></p></p><p><p><span style="font-size:18px;"></span></p></p><pre name="code" class="html"><! DOCTYPE html>

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.