1.xml
<?xml version= "1.0" encoding= "Utf-8"? ><webview xmlns:android= "Http://schemas.android.com/apk/res/android " android:id=" @+id/webview " android:layout_width=" Fill_parent " android: Layout_height= "Fill_parent"/>
2.activity
PackageCom.firefly.hybirdapp;Importandroid.content.Intent;ImportAndroid.net.Uri;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;Importandroid.webkit.WebSettings;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient;ImportAndroid.widget.Toast; Public classHomeactivityextendsappcompatactivity {WebView mywebview; //String loadurl = "File:///android_assets/index.html "; localString Neturl = "http://172.16.46.114:14023/index.html/"; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_home); Mywebview=(WebView) Findviewbyid (R.id.webview); Mywebview.loadurl (Neturl);//Load URL//Set WebViewMywebview.setwebviewclient (Newwebviewclient () {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true; } }); //Get Set classWebSettings ws =mywebview.getsettings (); Ws.setjavascriptenabled (true);//Allow JS script//write the method of JS call in WebAppMywebview.addjavascriptinterface (NewMywebappinterface ( This), "Android"); } //Press the back key to return to the previous page@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { //Check If the key event is the back button and if there's history if((keycode = = Keyevent.keycode_back) &&Mywebview.cangoback ()) {Mywebview.goback (); return true; } return Super. OnKeyDown (KeyCode, event); }}
3.WEBAPP class
PackageCom.firefly.hybirdapp;ImportAndroid.content.Context;ImportAndroid.util.Log;ImportAndroid.webkit.JavascriptInterface;/*** Created by Pengdan on 2016/6/27.*/ Public classMywebappinterface {context context; PublicMywebappinterface (Context context) { This. Context =context; } @JavascriptInterface Publicstring Lookdata (String data) {LOG.E ("TAG", Data + "hello!!"); //return information to HTML return"Hello"; }}
4.html
<! DOCTYPE html>Mui.init (); function look () {var ua=navigator.userAgent.toLocaleLowerCase (); Alert (UA); //Determine if Android if(/android/. Test (UA)) { //Send Messagevar msg = window.android.lookData (' I am HTML5 '); //write the received message into spandocument.getElementById (' span '). InnerHTML =msg; Alert (msg); } } </script> Hybird mixed-type development and construction