WebView in JS call Android Method encountered pit finishing

Source: Internet
Author: User

WebView is a common component of Android that is used to display Web pages and to allow Web pages and Android apps to interact with some business logic.

Its pit countless, believe used to know, one to solve it.

1. How to cross-tune:

<! doctype>{if (BL) {document.getElementById ("AP"). Innerhtml= "is true, the parameters are passed over."}Else{document.getElementById ("AP"). Innerhtml= "changed the text of the paragraph "}} function self (){document.getElementById ("btn"). innerhtml= "JavaScript Change button text"}</script>

The above is the JS code, the following is the Android code:

 Public classMywebviewtestextendsappcompatactivity {@Overrideprotected voidonCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.webviewtest_layout);    Initview (); }    Private voidInitview () {FinalWebView WebView =(WebView) Findviewbyid (R.id.my_web); Webview.loadurl ("File:///android_asset/webviewtest.html"); WebSettings websettings=webview.getsettings (); Websettings.setjavascriptenabled (true); Webview.addjavascriptinterface (NewJiaohu (), "Hello"); Button btn=(Button) Findviewbyid (R.ID.GET_JS); Btn.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Webview.loadurl ("Javascript:android (True)");    }        }); }     Public classjiaohu{@JavascriptInterface Public voidshowandroid () {Toast.maketext (mywebviewtest. This, "JS calls the Android method", Toast.length_short). Show (); }    }}

JS call Android Mode: <button onclick= "window.hello.showAndroid ()" > Click toast</button>

Hello refers to the Class (interface) alias, in our code above is the Jiaohu class, Showandroid () is the method name, so the Android and JS to agree that the class alias and the method name can be called successfully.

Android Call JS mode: Webview.loadurl ("javascript:android (true)");

Android () is a well-defined method name in JS, we can pass in the Android code a parameter true, in JS can receive, execute document.getElementById ("AP"). Innerhtml= " It is true that the argument passes over "this sentence."

2. One of the pits (the JS method in WebView cannot be performed):

When a colleague of the Web Group requests background data using the AJAX framework, I can't show it here for a few reasons:

Webview.getsettings (). setdomstorageenabled (true);  // very key!!! 

This code allows Android to read the JS node, it can also execute the

3. Pit II (WebView in H5 deformation):

The activities we do are similar to this

Then goose, when you click on the input box, pop the soft keyboard and turn it into this:

Yes, Mom's deformed. (Metamorphosis), this requires a front-end colleague to set the window's height to fixed length (before height=100%)

var height = $ (' body '). Height ();         {            $ ('. Main '). Height (height);        } );

Yes, this solves it.

Pit III (JS in Operation Android UI led to Java.lang.IllegalStateException:Calling View methods on another thread than the UI thread.at exception):

This is the most disgusting, not one that has not been encountered before.

Our activities to integrate Umeng share, share, share to the circle of friends no problem (more magical is to do when the time is OK, submitted to the test when the changes, the day the dog).

When it comes to a problem with manipulating the UI, we're definitely going to put JS in the UI thread that calls our code.

Runonuithread (new  Runnable () {    @Override     publicvoid  run () {         //    

This is naturally not a problem, that is, to put the content of our annotated method in the UI thread to run, but your class your interface He is not activity AH no Runonuithread method ah.

witted, let our class inherit Activity

 Public class extends Activity

OK, now it's okay, and then in that way

@JavascriptInterface      Public void Sharetofriend () {        runonuithread(new  Runnable () {            @Override            public  void  run () {                = drawabletobitmap (r.drawable.sharepic);                 = Umengshareutils.share (activity,msharecontroller,sharetitle,sharecontent,targeturl,bitmap);}}        );    } 

The pit is really a lot, and the line and careful.

WebView in JS call Android Method encountered pit finishing

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.