Use WebView to display Web pages in the interface via intent call system browser URI uri=uri.parse (URL);//url for the linked address intent intent=new intent (Intent.action_view,uri ); StartActivity (intent);----------------------------- Course Objective: Make a simple web browser through WebView course content: (1) Add WebView to your app (2) Use the WebView load page (3) for network access (configured in Androidmanifest) (4) Use JavaScript in WebView ( 5) Process the page load process (6) Back and forward (7) Determine the loading process of the page (8) Application of WebView cache (optimized for browser)
To add Wbeview to your app, simply add the <WebView> element to your active layout <webviewandroid:id= "@+id/webview" android:layout_width= "match _parent "android:layout_height=" match_parent "/> Load page, using Loadurl () Web resource: Webview.loadurl (" http://www.baidu.com ") Local resources: WebView. Loadurl ("file:///android_asset/xxx.html"); The local file is placed in the asset file//causes the page to get Focus Webview.requestfocus ();---------------------- --Get network access before it works, make sure your app has access to the network. To access the network, you need to get Internet permissions in the configuration file: <manifest ><uses_permission android:name= "Android.permission.INTERNET"/ >...</manifest>
Using JavaScript in WebView if you want to use JavaScript for a Web page loaded in WebView, you need to enable JavaScript in WebView and enable it by WebView with WebSettings. You can get the value of WebSettings through GetSettings () and then enable JavaScript through setjavascriptenabled (). webview= (WebView) Findviewbyid (R.id.webview); WebSettings websettings = Webview.getsettings ();
We use Toast () to display the address of the current page. When you click on the page to return, it may be because the Web page itself there is a request redirect, so may have jumped several pages, then we need to continue to click the Back button to really quit the program. Overwrite physical keys--Returns the logical @Override public boolean onKeyDown (int keycode, keyevent event) { if (keycode== Keyevent.keycode_back) {//keyevent.keycode_back is a constant, KeyEvent has many other constants Toast.maketext (this, Webview.geturl (), Toast.length_short). Show ()//show address if (Webview.cangoback ()) {//If you can return, return to previous page webview.goback (); return true; } else{//cannot return, the description is already the home page, so on the exit program system.exit (0);//Exit } } return Super.onkeydown (KeyCode, event); }
Websettings.setjavascriptenabled (TRUE);
1 Public classMainactivityextendsActivityImplementsOnclicklistener {2 3 PrivateString URL =NULL; 4 PrivateWebView WebView; 5 PrivateEditText Et_url; 6 PrivateButton Btn_login; 7 PrivateButton Btn_back; 8 PrivateButton Btn_exit; 9 PrivateButton Btn_forward; Ten PrivateButton Btn_menu; One A @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); the //Window progress bar - requestwindowfeature (window.feature_indeterminate_progress); - Setcontentview (R.layout.fragment_main); - +Setprogressbarindeterminate (true); - +WebView =(WebView) Findviewbyid (R.id.webview); AEt_url =(EditText) Findviewbyid (R.id.et_url); atBtn_login =(Button) Findviewbyid (R.id.btn_login); -Btn_back =(Button) Findviewbyid (r.id.btn_back); -Btn_exit =(Button) Findviewbyid (r.id.btn_exit); -Btn_forward =(Button) Findviewbyid (R.id.btn_forward); -Btn_menu =(Button) Findviewbyid (R.id.btn_menu); - //Add a click-Listen event to five buttons inBtn_login.setonclicklistener ( This); -Btn_back.setonclicklistener ( This); toBtn_exit.setonclicklistener ( This); +Btn_forward.setonclicklistener ( This); -Btn_menu.setonclicklistener ( This); the } * $ //Btn_login Trigger Event Click WebView Start reading URLPanax Notoginseng protected voidstartreadurl (String url) { - //TODO auto-generated Method Stub the //WebView Loading Web Resources + webview.loadurl (URL); A //Overwrite webview default behavior of opening Web pages via system or third-party browser the //If you call the system or the third-party browser to open the Web page behavior +Webview.setwebviewclient (Newwebviewclient () { - @Override $ Public Booleanshouldoverrideurlloading (WebView view, String URL) { $ //TODO auto-generated Method Stub - //WebView Loading Web Resources - view.loadurl (URL); the return true; - } Wuyi }); the //Enable JavaScript support -WebSettings settings =webview.getsettings (); WuSettings.setjavascriptenabled (true); - //Web Load page takes precedence over cache loading About Settings.setcachemode (websettings.load_cache_else_network); $ //Show progress bar when opening page open full hide progress bar -Webview.setwebchromeclient (Newwebchromeclient () { - @Override - Public voidOnprogresschanged (WebView view,intnewprogress) { A //TODO auto-generated Method Stub +Settitle ("This page has been loaded" + newprogress + "%"); the if(newprogress = = 100) { - Closeprogressbar (); $}Else { the Openprogressbar (newprogress); the } the Super. onprogresschanged (view, newprogress); the } - }); in } the the //Open progress bar About protected voidOpenprogressbar (intx) { the //TODO auto-generated Method Stub theSetprogressbarindeterminatevisibility (true); the setprogress (x); + } - the //Close the progress barBayi protected voidCloseprogressbar () { the //TODO auto-generated Method Stub theSetprogressbarindeterminatevisibility (false); - } - the //rewriting the logic of the physical key return key the @Override the Public BooleanOnKeyDown (intKeyCode, KeyEvent event) { the //TODO auto-generated Method Stub - if(KeyCode = =keyevent.keycode_back) { the if(Webview.cangoback ()) { the //back to previous page the Webview.goback (); 94 return true; the}Else { the //Exit Program the finish (); 98 } About } - return Super. OnKeyDown (KeyCode, event); 101 } 102 103 //Handling of button events104 @Override the Public voidOnClick (View v) {106 //TODO auto-generated Method Stub107 Switch(V.getid ()) {108 CaseR.id.btn_login:109url = "/http" +Et_url.gettext (). toString (); theurl = Url.replace ("", "" "); 111 startreadurl (URL); the Break; 113 CaseR.id.btn_back: the if(Webview.cangoback ()) { the Webview.goback (); the}Else { 117 finish (); 118 } 119 Break; - CaseR.id.btn_forward:121 if(Webview.cangoforward ()) {122 Webview.goforward (); 123 } 124 Break; the CaseR.id.btn_exit:126 finish (); 127 Break; - CaseR.id.btn_menu:129Startreadurl ("http://www.baidu.com"); the Break; 131 } the } 133}
Android Siege Lion WebView (Show page)