Interaction between java and js in webview, and webviewjavajs
WebView provides powerful functions for displaying web pages in Android applications. It is also the basis for the vigorous development of the Hybird app. As a very important component of the Android system, it provides two powerful capabilities: HTML parsing, layout, and rendering; and JavaScript interpretation and execution. Hybird App is composed of Native + H5, Native is implemented in java, and JavaScript is an essential part of H5. Therefore, Java and JavaScript call each other! Here is a basic example of mutual calls!
1. Add the WebView component to the Native Layout
1 <WebView2 android:id="@+id/wv_contacts"3 android:layout_below="@id/tv_title"4 android:layout_width="match_parent"5 android:layout_height="match_parent">6 </WebView>
2. initialize WebView, set to allow JavaScript and load pages
1 private void initWebView() {2 mWebView = (WebView) findViewById(R.id.wv_contacts);3 WebSettings settings = mWebView.getSettings();4 settings.setJavaScriptEnabled(true);5 mWebView.loadUrl("file:///android_asset/constacts.html");6 }
3. java calls javaScript (mWebView. loadUrl ("javascript: method (param )");)
First, define the JavaScript function:
1 function showData(constactsData){2 var html="";3 var ullist = document.getElementById("contacts_list");4 var constactsJsonData = eval(constactsData);5 for(var i = 0; i < constactsJsonData.length; i++){6 html += "<li onclick=callPhone(\"" + constactsJsonData[i].number + "\")>" + constactsJsonData[i].name + "</li>";7 }8 ullist.innerHTML = html;9 }
Then, JavaScript is called in java and put in the onPageFinished callback to ensure that all Js loads have been completed when JavaScript is called.
1 mWebView.setWebViewClient(new WebViewClient() { 2 @Override 3 public boolean shouldOverrideUrlLoading(WebView view, String url) { 4 view.loadUrl(url); 5 return true; 6 } 7 8 @Override 9 public void onPageFinished(WebView view, String url) {10 super.onPageFinished(view, url);11 showContactsInfo();12 }13 });14 15 private void showContactsInfo() {16 String info = jsInterface.readContacts();17 mWebView.loadUrl("javascript:showData(" + info + ")");18 }
4. Call Java (mWebView. addJavascriptInterface (new JavaScriptInterface (), "interface");) in Js ");). As shown in the code, call the Java Native method in Js. You need to convert the object of the method to an alias. Upload the alias to JavaScript and then access the Native method through the alias in JavaScript.
Add an alias first
1 mWebView. addJavascriptInterface (new JSInterface (this. getApplicationContext (), "jsinterface ");
Then define the Native METHOD
1 public void callPhone(String number) {2 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));3 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);4 mContext.startActivity(intent);5 }
Finally, use the alias to call in JavaScript.
1 function callPhone (number) {2 jsinterface. callPhone (number); 3}
If you have completed the most basic Java and JavaScript communication functions in the Hybird App!