WebView can make the Web page easy to embed into the app, but also directly with JS to call each other.
WebView has two methods: Setwebchromeclient and Setwebclient
Setwebclient: Main processing parsing, rendering Web browsers and other things to do
Setwebchromeclient: Auxiliary webview Processing JavaScript dialog box, website icon, website title, loading progress, etc.
Webviewclient is to help WebView handle various notifications, request events.
To set access network permissions in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.INTERNET"/>
Control:
<webview android:layout_width= "match_parent" android:layout_height= "match_parent" android:id= "@ +id/webview " />
Use one: Load local/web resources
Example.html stored in Assets folder
Call WebView's Loadurl () method,
Load Local Resources
WebView = (WebView) Findviewbyid (R.id.webview); Webview.loadurl ("file:///android_asset/example.html");
To load a Web resource:
WebView = (WebView) Findviewbyid (R.id.webview); Webview.loadurl ("http://baidu.com");
Purpose Two: Open the Web page within the program
Create a webviewclient of your own, by Setwebviewclient Association
Package Com.example.testopen;import Android.app.activity;import Android.os.bundle;import Android.webkit.webview;import Android.webkit.webviewclient;public class Mainactivity extends Activity {private WebView WebView; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.test); Init (); } private void Init () {WebView = (WebView) Findviewbyid (R.id.webview); WebView Load Web resource webview.loadurl ("http://baidu.com"); Overwrite WebView default use of third-party or system default browser to open the Web page behavior, so that the Web page with WebView Open webview.setwebviewclient (New Webviewclient () {@Override public boolean shouldoverrideurlloading (WebView view, String URL) {//TODO auto-generated method stub The return value is true when the control goes to WebView open, to False to invoke the system browser or the third-party browser view.loadurl (URL); return true; } }); } }
Use three:
If you have JavaScript on the page you are visiting, WebView must set up JavaScript support
Enable support javascriptwebsettings settings = Webview.getsettings (); settings.setjavascriptenabled (true);
Use four:
If you want to browse the page back instead of exiting the browser, you need to webview overwrite the URL to load, let it automatically generate historical access records, so that you can go forward or back to access the site has been visited.
Overwrite physical keys--return logic @Override public boolean onKeyDown (int keycode, keyevent event) { //TODO auto-generated Method Stub if (Keycode==keyevent.keycode_back) { if (Webview.cangoback ()) { webview.goback () ;//Return to the previous page return true; } else { system.exit (0);//Exit Program } } return Super.onkeydown (KeyCode, event); }
Use five: Judge the page loading process
Webview.setwebchromeclient (New Webchromeclient () { @Override public void onprogresschanged (webView view, int newprogress) { //TODO auto-generated method Stub if (newprogress = =) { //page load complete } else { //load in c8/>} } );
Purpose VI: Use of the cache
Prioritize using caching
Webview.getsettings (). Setcachemode (Websettings.load_cache_else_network);
Do not use cache:
Webview.getsettings (). Setcachemode (Websettings.load_no_cache);
Related:
http://www.imooc.com/video/2269
Http://www.cnblogs.com/tinyphp/p/3858997.html
Android: Control webview Display Web page