To set access network permissions in Androidmanifest.xml:
<span style= "FONT-SIZE:24PX;" ><span style= "FONT-SIZE:24PX;" ><uses-permission android:name= "Android.permission.INTERNET"/></span></span>
Control:
<span style= "FONT-SIZE:24PX;" ><span style= "FONT-SIZE:24PX;" ><webview android:layout_width= "match_parent" android:layout_height= "Match_parent" android: Id= "@+id/webview" /></span></span>
Key code:
<span style= "FONT-SIZE:24PX;" > mwebview= (WebView) Findviewbyid (R.id.webview); Mwebview.setvisibility (view.visible); WebSettings websettings = Mwebview.getsettings (); Websettings.setjavascriptenabled (true); Mwebview.loadurl ("http://www.baidu.com"); </span>
But if you just use the code above to invoke the browser to load the Web page by default, instead of loading the page in WebView
method is to use the Setwebviewclient
<span style= "FONT-SIZE:24PX;" >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; } }); }}</span>3. If there is JavaScript on the page visited, the WebView must be set to support JavaScript
<span style= "FONT-SIZE:24PX;" >//Enable support javascriptwebsettings settings = Webview.getsettings (); settings.setjavascriptenabled (true); </span >
4, if you want to browse the page back instead of exiting the browser, you need to WebView overwrite URL loading, let it automatically generate historical access records, so that you can go forward or back to visit the site has been visited.
<span style= "FONT-SIZE:24PX;" >//rewrite 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); } </span>
5, determine the page loading process
<span style= "FONT-SIZE:24PX;" >webview.setwebchromeclient (New Webchromeclient () { @Override public void onprogresschanged (WebView view , int newprogress) { //TODO auto-generated method Stub if (newprogress = =) { //page load complete } else { / /Load }} );</span>
WebView has two methods: Setwebchromeclient and Setwebclient
Setwebviewclient: 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.
Webchromeclient is an auxiliary webview processing JavaScript dialog box, website icon, website title, loading progress etc.
Onclosewindow (Close WebView)
Oncreatewindow ()
Onjsalert (WebView on alert is not something to play, need to customize your webchromeclient processing pop-up)
Onjsprompt
Onjsconfirm
Onprogresschanged
Onreceivedicon
Onreceivedtitle
For example, you can add a progress bar to make the interface more friendly
<span style= "FONT-SIZE:24PX;" >webview1.setwebchromeclient (New Webchromeclient () {public void onprogresschanged (WebView view, int Progress) { setprogress (progress *); if (progress = =) { imageview1.setvisibility (view.gone); Tv1.setvisibility (view.gone); Pb1.setvisibility (view.gone); Fy1.setvisibility (View.gone);}} ); </span>
webviewclient is to help WebView handle various notifications, request events, specifically including:
Onloadresource
Onpagestart
Onpagefinish
Onreceiveerror
Onreceivedhttpauthrequest
<span style= "FONT-SIZE:24PX;" >webview1.setwebviewclient (New Webviewclient () {public void Onreceivederror (WebView view, int errorCode, String description, String failingurl) {//Handle the error Toast.maketext (Getapplicationcontext (), "Network connection failed, Please connect to the network. ", Toast.length_short). Show ();} public boolean shouldoverrideurlloading (WebView view, String URL) { view.loadurl (URL); return true;}}); Sets the character set encoding webview1.getsettings (). Setdefaulttextencodingname ("UTF-8"); Webview1.loadurl ("http://www.android100.org/");</span>
Android uses WebView to load Web pages