Most of the time we use HTML layout to be more convenient and direct, record a bit.
I now mainly directly invoke the server's Web page (actually JSP, just return HTML), so need to network, the first step to add permissions.
<uses-permission android:name= "Android.permission.INTERNET"/>
The layout file is directly used with a webview, as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <webview android:id= "@+id/webview1" android:layout_width= "Match_ Parent " android:layout_height=" Match_parent "/></linearlayout>
You can write the code directly below:
Package Com.yangshidesign.testgryoscope;import Android.app.activity;import Android.os.bundle;import Android.util.log;import Android.webkit.javascriptinterface;import Android.webkit.webview;import Android.widget.toast;public class Addemojiactivity extends Activity {private WebView WebView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); This.setcontentview (r.layout.activity_ Addemoji); WebView = (WebView) This.findviewbyid (r.id.webview1); Webview.getsettings (). Setjavascriptenabled (True); Webview.addjavascriptinterface (New Webplugin (), "Webplugin"); Webview.loadurl (This.getstring (R.string.server_url) );} /** * Plug-in class, in the HTML JS inside directly call */private class Webplugin {@JavascriptInterfacepublic void Test () {LOG.E ("Miquan", "kkkkkk"); Toast.maketext (addemojiactivity.this, "Test Toast", Toast.length_short). Show (); @JavascriptInterfacepublic String test2 () {return ' Something ';}}}
one of the @javascriptinterface annotations is added to each method that needs to be used.
Finally, you can call it directly on the HTML page.
<script type= "Text/javascript" >function Test () {webplugin.test (); var something = Webplugin.test2 ();} </script>