<!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML> <Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"> <title>Insert Title here</title> <Scripttype= "Text/javascript"> functionShow (jsondata) {varJsonobjs=eval (jsondata); varTable=document.getElementById ("persontable"); for(vary=0; y<jsonobjs.length; y++){ varTR=Table.insertrow (table.rows.length); varTD1=Tr.insertcell (0); varTD2=Tr.insertcell (1); Td2.align= "Center"; Td1.innerhtml=Jsonobjs[y].name; Td2.innerhtml= "<a href= ' Javascript:Android.call (\ ""+Jsonobjs[y].phone+ "\ ") ' >"+Jsonobjs[y].phone+ "</a>"; } } </Script> </Head> <Bodyonload= "javascript:Android.showcontacts ()"> <DivAlign= "Left">Welcome to visit</Div> <P><imgsrc= "Http://atguigu.com/images/logo.gif"></P> <TableBorder= "0"width= "100%"ID= "Persontable"cellspacing= "0"> <TR> <TDwidth= "30%">Name</TD> <TDAlign= "Center">Phone</TD> </TR> </Table> </Body></HTML>
Public classJscalljavacallphoneactivityextendsActivity {PrivateWebView WebView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_js_call_java_video); WebView=(WebView) Findviewbyid (R.id.webview); WebSettings websettings=webview.getsettings (); //set support JavaScript footstep languageWebsettings.setjavascriptenabled (true); //Double-click Support-provided the page is supported for display//Websettings.setusewideviewport (TRUE); //Support for zoom buttons-provided the page is supported for displayWebsettings.setbuiltinzoomcontrols (true); //set client-do not jump to the default browserWebview.setwebviewclient (Newwebviewclient ()); //Set Support JS call JavaWebview.addjavascriptinterface (NewAndroidandjsinterface (), "Android"); //Load Local Resources//Webview.loadurl ("http://atguigu.com/teacher.shtml");Webview.loadurl ("file:///android_asset/JsCallJavaCallPhone.html");//Webview.loadurl ("http://10.0.2.2: 8080/assets/jscalljavacallphone.html "); } classAndroidandjsinterface {/*** This method will be called by JS to load the data*/@JavascriptInterface Public voidshowcontacts () {//The following code recommends calling a child thread in theString json = "[{\" name\ ": \" Ford \ ", \" phone\ ": \" 18600012345\ "}]"; //invoking the method in JSWebview.loadurl ("Javascript:show ('" + JSON + "')"); } /*** Call *@paramPhone*/@JavascriptInterface Public voidCall (String phone) {Intent Intent=NewIntent (Intent.action_call, Uri.parse ("Tel:" +phone)); if(Activitycompat.checkselfpermission (jscalljavacallphoneactivity. This, Manifest.permission.CALL_PHONE)! =packagemanager.permission_granted) { return; } startactivity (Intent); } }}
Java and JS Intermodulation calls