Android WebView Development

Source: Internet
Author: User

 

A high-performance webkit kernel browser is built in the Android mobile phone. It is encapsulated as a WebView component in the SDK.

 

What is webkit

 

WebKit is a software framework included in Mac OS X v10.3 and later versions (for v10.2.7 and later versions, you can also obtain it through software updates ). WebKit is also the foundation of Mac OS X's Safari web browser. WebKit is an open-source project. It is mainly modified by KDE's KHTML and contains some components from Apple.

 

Traditionally, WebKit contains a web engine WebCore and a script engine JavaScriptCore, which correspond to KDE's KHTML and KJS respectively. However, as JavaScript Engines become more and more independent, WebKit and WebCore are now basically mixed (for example, Google Chrome and Maxthon 3 adopt V8 engines, but they still claim to be WebKit kernels ).

 

Here we will first try to use webview to browse Web pages in android. There is a simple example of WebView In SDK Dev Guide.

 

Pay attention to the following points during development:

1. AndroidManifest. xml must use the permission "android. permission. INTERNET". Otherwise, the Web page not available error may occur.

2. If the accessed page contains Javascript, webview must be set to support Javascript.

Webview. getSettings (). setJavaScriptEnabled (true );

3. If you want to click the link on the page to continue responding in the current browser, instead of making a response in the browser of the new Android system, you must overwrite the WebViewClient object of webview.

MWebView. setWebViewClient (new WebViewClient (){

Public boolean shouldOverrideUrlLoading (WebView view, String url ){

View. loadUrl (url );

Return true;

}

});

/**

* 4. If you use the webview link to view many pages, if you do not perform any processing, click the system "Back" key, and the entire browser will call finish () to finish itself,

* If you want to roll Back the web page to be viewed rather than exit the browser, you need to process and consume the Back event in the current Activity.

* Override the onKeyDown (int keyCoder, KeyEvent event) method of the Activity class.

*/

Public boolean onKeyDown (int keyCoder, KeyEvent event ){

If (webView. canGoBack () & keyCoder = KeyEvent. KEYCODE_BACK ){

WebView. goBack (); // goBack () indicates that the previous page of webView is returned.

Return true;

}

Return false;

}

 

 

The Code is as follows:

 

1. add permissions: The permission "android. permission. INTERNET" must be used in AndroidManifest. xml. Otherwise, the Web page not available error may occur.

 

 

2. AboutUSActivity. java

Package com.cn. view;

Import com.cn. mr. http. util. Configuration;

Import android. app. Activity;

Import android. app. ProgressDialog;

Import android. OS. Bundle;

Import android. view. KeyEvent;

Import android. webkit. WebChromeClient;

Import android. webkit. WebView;

Import android. webkit. WebViewClient;

Import android. webkit. SslErrorHandler;

 

// About us

Public class AboutUSActivity extends Activity {

Final String mimeType = "text/html ";

Final String encoding = "UTF-8 ";

ProgressDialog progresDialog = null;

WebView webView;

Public static final String ABOUTUS = "http://phone.tomome.net/aboutus.html ";

 

 

Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );

SetContentView (R. layout. aboutus );

ProgresDialog = new ProgressDialog (this );

ProgresDialog. setMessage ("Page request .......");

ProgresDialog. show ();

WebView = (WebView) findViewById (R. id. webviewaboutus );

WebView. getSettings (). setJavaScriptEnabled (true );

WebView. loadUrl (ABOUTUS );

WebView. setWebChromeClient (new WebChromeClient (){

@ Override

Public void onProgressChanged (WebView view, int newProgress ){

If (newProgress = 100) {// set the title of the activity,

// You can also perform other operations based on your needs.

ProgresDialog. setMessage ("loaded ");

ProgresDialog. cancel ();

} Else {

ProgresDialog. setMessage ("loading .......");

}

}

});

// WebView. setWebViewClient (new WebViewClient (){

// @ Override

// Public boolean shouldOverrideUrlLoading (WebView view, String url) {// rewrite this method to indicate that clicking the link in the webpage is still redirected to the current webview, not to the browser

// View. loadUrl (url );

// Return true;

//}

//

// @ Override

// Public void onReceivedSslError (WebView view,

// SslErrorHandler handler, android.net. http. SslError error) {// rewrite this method to allow webview to process https requests

// Handler. proceed ();

//}

//

//});

} Www.2cto.com

 

/**

* If you use the webview link to view many pages without any processing, click the system "Back" key and the entire browser will call finish () to finish itself,

* If you want to roll Back the web page to be viewed rather than exit the browser, you need to process and consume the Back event in the current Activity.

* Override the onKeyDown (int keyCoder, KeyEvent event) method of the Activity class.

*/

Public boolean onKeyDown (int keyCoder, KeyEvent event ){

If (webView. canGoBack () & keyCoder = KeyEvent. KEYCODE_BACK ){

WebView. goBack (); // goBack () indicates that the previous page of webView is returned.

Return true;

}

Return false;

}

}

 

3. XML layout file:

 

<? Xml version = "1.0" encoding = "UTF-8"?>

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical"

Android: layout_width = "fill_parent" android: layout_height = "fill_parent">

<WebView android: layout_height = "fill_parent" android: layout_width = "fill_parent" android: id = "@ + id/webviewaboutus"> </WebView>

</LinearLayout>

 

By Chen Xiaohui

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.