Detailed explanation of WebView in Android:
1. Concept:
WebView (network view) can load a display page, which can be viewed as a browser. It uses the WebKit rendering engine to load the Display Web page.
2. How to use:
(1). Instantiate the WebView component:
A. Instantiate the WebView component in the activity. eg
Copy Code code as follows:
WebView WebView = new WebView (this);
B. Call the WebView Loadurl () method to set the page to be displayed wevview. Eg:
Copy Code code as follows:
Internet use: Webview.loadurl ("http://www.google.com");
Local file use: Webview.loadurl ("file:///android_asset/XX.html");
Local files stored in: Assets files
C. Invoke the Setcontentview () method of the activity to display the Web page view.
D. You need to add permissions to the Androidmanifest.xml file, otherwise the Web page not available error occurs.
Copy Code code as follows:
<uses-permission android:name= "Android.permission.INTERNET"/>
(2). Custom WebView component Inherits Webviewclient:
A. Declare the webview in the layout file. eg
Copy Code code as follows:
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<webview
Android:id= "@+id/webview1"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
</LinearLayout>
B. Instantiation of webview in the activity.
C. Call the WebView Loadurl () method to set the page to be displayed webview.
D. Call the Setwebviewclient () method to set the WebView view. Response link functionality.
E. You need to add permissions in the Androidmanifest.xml file, otherwise the Web page not available error occurs.
Copy Code code as follows:
<uses-permission android:name= "Android.permission.INTERNET"/>
3. The difference between two methods:
(1). The first method: Click on the link is the new Android system browser should be linked to the ring.
(2). The second method: Click on the link to be handled by yourself, rather than the new Android system browser should be linked in the ring. Add an Event Listener object (webviewclient) to WebView and override the Shouldoverrideurlloading method in the response to the hyperlink button in the Web page. When a connection is pressed, Webviewclient calls this method and passes the argument: the pressed URL.
4. Summary:
(1). With WebView point link to see a lot of pages later in order to allow WebView support fallback function, need to cover the Activity Class onkeydown () method, if do not do any processing, click the system back-shear key, the entire browser will call finish () and end itself, Instead of retreating back to the previous page.
Copy Code code as follows:
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {
GoBack () indicates that the previous page of WebView is returned
Mwebview.goback ();
return true;
}
Return Super.onkeydown (KeyCode, event);
}
(2). Set WebView basic information:
A. If you have JavaScript on the page you are visiting, WebView must set up to support JavaScript.
Copy Code code as follows:
Webview.getsettings (). Setjavascriptenabled (True);
B. Touch Focus Works:
Copy Code code as follows:
C. Cancel the scroll bar:
Copy Code code as follows:
This.setscrollbarstyle (Scrollbars_outside_overlay);
5. The overall code is as follows:
(1). Mainactivity.java
Package Com.pansoft.webviewdemo;
Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.KeyEvent;
Import Android.view.Window;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import Com.pansoft.webviewdemo.webView.MyWebView;
public class Mainactivity extends activity {
Private WebView Mwebview = null;
Private websettings msettings = null;
/** TAG */
Private String TAG = GetClass (). Getsimplename ();
/** URL * *
Private String Flg_url = "http://www.baidu.com/";
Private Mywebview Mywebview;
@SuppressLint ("setjavascriptenabled")
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
The second method
INIT01 ();
The first of these methods
Init02 ();
}
/**
* The load is webview itself.
*/
private void init01 () {
Setcontentview (R.layout.activity_main);
Mwebview = (webview) Findviewbyid (R.ID.WEBVIEW1);
Msettings = Mwebview.getsettings ();
WebView Settings Support JavaScript
Msettings.setjavascriptenabled (TRUE);
Load URL
Mwebview.loadurl (Flg_url);
Mywebview = new Mywebview (this, mwebview);
Mwebview.setwebviewclient (Mywebview);
}
/**
* The system is loaded with its own browser
*/
private void init02 () {
Mwebview = new WebView (this);
Msettings = Mwebview.getsettings ();
Msettings.setjavascriptenabled (TRUE);
Mwebview.loadurl (Flg_url);
Setcontentview (Mwebview);
}
@Override
public void onbackpressed () {
Super.onbackpressed ();
}
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {
GoBack () indicates that the previous page of WebView is returned
Mwebview.goback ();
return true;
}
Return Super.onkeydown (KeyCode, event);
}
}
(2). Mywebview.java
Package Com.pansoft.webviewdemo.webView;
Import Android.content.Context;
Import Android.graphics.Bitmap;
Import Android.util.Log;
Import Android.webkit.HttpAuthHandler;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;
/**
* Mywebview
*
* @author Administrator
*
*/
public class Mywebview extends Webviewclient {
Private context Mcontext;
Private WebView Mwebview;
Private String TAG = GetClass (). Getsimplename ();
/**
* Construction Method
*
& nbsp * @param mcontext
* @param mwebview
*/
Public Mywebview (Context Mcontext, WebView Mwebview) {
super ();
this.mcontext = Mcontext;
this.mwebview = Mwebview;
}
/**
* Open the link before the event, in order to avoid the time to press the load is the system's own browser, click on the link by their own processing
*/
This function we can do a lot of things, such as we read some special URL, so we can not open the address, cancel this operation, for predefined other operations, which is very necessary for a program.
@Override
public boolean shouldoverrideurlloading (webview view, String URL) {
if (URL!= null) {
Mwebview.loadurl (URL);
LOG.D (TAG, "--->shouldoverrideurlloading--->");
}
return true;
}
/**
* An event that receives an HTTP request
*/
@Override
public void Onreceivedhttpauthrequest (WebView view,
Httpauthhandler handler, string host, String realm) {
Super.onreceivedhttpauthrequest (view, Handler, host, realm);
}
/**
* Load the start of the page event
*/
This event is the start of loading the page call, and usually we can set a loading page here, telling the user that the program is waiting for the network to respond.
@Override
public void onpagestarted (webview view, String URL, Bitmap favicon) {
super.onpagestarted (view, URL, favicon);
LOG.D (TAG, "--->onpagestarted--->");
}
/**
* Loading Page completion events
*/
In the same way, we know that a page load is complete, so we can turn off the loading and switch the program action.
@Override
public void onpagefinished (webview view, String URL) {
super.onpagefinished (view, URL);
LOG.D (TAG, "--->onpagefinished--->");
}
/**
* Notify us of applications, such as network errors, when an error occurs in the Web site where the browser is making a visit.
*/
@Override
public void Onreceivederror (webview view, int errorcode,
String description, String failingurl) {
Super.onreceivederror (view, errorcode, description, Failingurl);
}
}
(3). Activity_main.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >
<webview
Android:id= "@+id/webview1"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"/>
</LinearLayout>
(4). Permission:
<uses-permission android:name= "Android.permission.INTERNET"/>