WebView and JavaScript implement data interaction examples in Android _android

Source: Internet
Author: User

The first thing to do is to have JavaScript basics before you can read this article.

(1) JS calls the Android method:

Copy Code code as follows:
WebView Wview;
Wview. Addjavascriptinterface (Object obj, String InterfaceName);

is an instantiation of an object, called in the HTML JS, the second parameter is the alias of the instantiated object, if you want to use this obj, the name used in JS
Is InterfaceName.
Copy Code code as follows:
public class Jswebdemo extends activity {
WebView Wview;

   /** Called when the activity is a-created */ 
    @Override  
    public void OnCreate (Bundle savedinstancestate) {  
     & nbsp;  super.oncreate (savedinstancestate);  
        Setcontentview (r.layout.main);   
        Wview = (webview) Findviewbyid (R.ID.WV1);  
          
        websettings WSet = wview.getsettings ();  
         wset.setjavascriptenabled (true);  

Proxybridge Pbridge = new Proxybridge ();
Wview.addjavascriptinterface (Pbridge, "Aliansbridge");

Wview.loadurl ("file:///android_asset/index.html");

}

Private class Proxybridge {
public int One () {
return 1;
}
}

}


Take a look at the contents of index.html:
Copy Code code as follows:

<mce:script language= "JavaScript" ><!--

/* This function are invoked by the activity * *
Function Wave () {
Alert ("1");
document.getElementById ("Droid"). src= "Android_waving.png";
Alert ("2");
}

--></mce:script>
<body>
<div id= "Output" >test page.</div>

<a onclick= "window.demo.clickOnAndroid ()" >
<div style= "width:100px;
margin:0px Auto;
padding:10px;
Text-align:center;
border:2px solid #202020; ">
<br>
Click me!
</div>
</a>
<input type= "Submit" value= "Change to 1" onclick= "document.getElementById (' output '). InnerHTML

=aliansbridge.one () "/>
</body>


The Aliansbridge here is the Java object that is invoked.
Note that if you have only one way to be called by JS only a new object will be OK:
Proxybridge Pbridge = new Proxybridge ();
Wview.addjavascriptinterface (Pbridge, "Aliansbridge");
If you need to invoke multiple methods, you must instantiate the entire Android program's class itself:
Wview.addjavascriptinterface (This, "Aliansbridge");
(2) Android calls JS method:Wview.setwebchromeclient (webchromeclient client)
To use the Handler update ui,handler mhandler = new Handler ();
Copy Code code as follows:
Mwebview.setwebchromeclient (New Mywebchromeclient ());
Mwebview.addjavascriptinterface (New Object () {
/**
* This isn't called on the UI thread. Post a runnable to invoke
* Loadurl on the UI thread.
*/
public void Clickonandroid () {
Mhandler.post (New Runnable () {
public void Run () {
Mwebview.loadurl ("Javascript:wave ()");
}
});
}
}, "demo");

Similarly, there must be an object called demo in HTML to invoke the Clickonandroid () method.
Copy Code code as follows:
/**
* 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) {
Result.confirm ();
Return Super.onjsalert (view, URL, message, result);
}
}

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.