There are two ways to load WebView,
Load Network Address
Webview.loadurl ("www.xxx.com/index.html");
Load Local Resources
Webview.loadurl ("file:///android_asset/example.html");
- Add request header information, where you can add authentication information, etc.
map<string,string> map=New hashmap<string,string>() map.put ("Taken", "1234"); Webview.loadurl ("Http://192.168.99.101:8080/xiaoyanAPI/test1", map);
- Set the link in the page to jump, intercept the jump event, in the corresponding function to do the corresponding processing.
Webview.setwebviewclient (new webviewclient () { publicboolean Shouldoverrideurlloading (WebView view, String URL) { view.loadurl (URL); return true ; } });
- Set Allow JS method to execute
WebSettings websettings=webview.getsettings (); Websettings.setjavascriptenabled (true);
- The return key returns to the previous page (the default WebView the page into the cache stack)
@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { if(KeyCode = =keyevent.keycode_back) { if(Webview.cangoback ()) {webview.goback ();//go back to previous browse page return true; } Else{finish ();//Close Activity } } return Super. OnKeyDown (KeyCode, event); }
- The JS method of calling HTML on Android
Call the no return value method to remove the back of the function, which accepts the data returned by the JS function.
New Valuecallback<string>() { @Override publicvoid Onreceivevalue ( String value) { "onreceivevalue value=" + value); } });
- JS calls the original method of Android.
Above Android4.2 can be declared directly using @javascriptinterface annotations, the following is a local Java method
Public class jsinteration { @JavascriptInterface public String back () { return " Hello world "; }}
After you define this method, call the Mwebview.addjavascriptinterface () method:
Mwebview.addjavascriptinterface (new jsinteration (), "Android");
So how to invoke in JS?
<script type= "Text/javascript" > function S () { // call Java Back () method var result =Window.android.back (); document.getElementById ("P"). innerhtml=result; } </script>
Android native and Hml interaction (webview Base)