Android WebView JS use
1. JS calls Java
1, 1 JS code
<script type= "Text/javascript" >
function call () {
Window.androidInterface.call (' 02585818031 ');
}
</script>
1.2. Java code
Package Com.example.webview;
Import Android.annotation.SuppressLint;
Import android.content.Intent;
Import Android.net.Uri;
Import Android.os.Bundle;
Import android.support.v7.app.ActionBarActivity;
Import android.view.KeyEvent;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.Window;
Import Android.webkit.JavascriptInterface;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;
@SuppressLint ("Javascriptinterface")
public class Mainactivity extends Actionbaractivity {
Private WebView WebView;
private int screenheight;
private int screenwidth;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title); Set Untitled
Setcontentview (R.layout.activity_main);
WebView = (WebView) Findviewbyid (R.id.web_view);
WebSettings websettings = Webview.getsettings ();
Websettings.setbuiltinzoomcontrols (TRUE);
Websettings.setloadwithoverviewmode (TRUE);
Websettings.setjavascriptenabled (TRUE);
Webview.setwebviewclient (New Mywebviewclient ());
Webview.addjavascriptinterface (New Myandroidinterface (), "androidinterface");
String url = "file:///android_asset/tangbangjidian/fuwu.html";
String url = "file:///android_asset/baitian/index.html";
Webview.loadurl (URL);
}
Class myandroidinterface{
Public Myandroidinterface () {};
@JavascriptInterface//Note: Add this line of annotations
public void Call (String number) {
Start a phone call with intent
Intent Intent = new Intent (Intent.action_call,uri.parse ("Tel:" +number));
StartActivity (Intent);
}
}
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if ((keycode = = keyevent.keycode_back) && webview.cangoback ()) {
Webview.goback (); GoBack () indicates that the previous page of the WebView is returned
return false;
}
return true;
}
Private class Mywebviewclient extends webviewclient{
@Override
public boolean shouldoverrideurlloading (WebView view, String URL) {
View.loadurl (URL); When you open a new link, the current WebView is used and no other browser in the system is used
return true;
}
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
}
2, Java call JS
2.1, Java code, WebView for WebView object
No parameter calls
Webview.loadurl ("Javascript:javacalljs ()");
Passing parameter calls
Webview.loadurl ("Javascript:javacalljswithargs" ("+" ' Hello World ' "+") ");
2.2. JS Code
function Javacalljs () {
document.getElementById ("Content"). InnerHTML + =
"<br\>java called the JS function";
}
function Javacalljswithargs (ARG) {
document.getElementById ("Content"). InnerHTML + =
("<br\>" +arg);
}
Android WebView Java and JS call each other