Android WebView Java and JS call each other

Source: Internet
Author: User

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

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.