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