(a) What is Android WebView?

Source: Internet
Author: User

1.Android WebView some basic concepts

a high-performance WebKit kernel browser is built into the Android phone, encapsulated in the SDK as a component called WebView.
What is WebKit
WebKit is a software framework included with MAC OS X v10.3 and above (available through software updates for v10.2.7 and above versions). With
, WebKit is also the basis for the Mac OS X Safari web browser. WebKit is an open source project, modified primarily by KDE's khtml and
and contains some of the components from Apple.
Traditionally, WebKit contains a Web engine WebCore and a scripting engine javascriptcore, respectively, that correspond to the KDE khtml
and KJS. However, as the JavaScript engine becomes more independent, WebKit and WebCore are now largely mixed (for example, Google
Chrome and Maxthon 3 use the V8 engine, but still claim to be the WebKit kernel).
Here we have a preliminary experience on Android is using WebView to browse the Web, in the SDK Dev Guide has a WebView simple
example.
In the development process should pay attention to several points:
License "Android.permission.INTERNET" must be used in 1.androidmanifest.xml, otherwise the WEB page not available error will occur
2. If there is JavaScript on the page visited, the WebView must be set to support JavaScript.
Webview.getsettings (). Setjavascriptenabled (True);
3. If the page links, if you want to click on the link to continue in the current browser response, instead of the new Android system browser should link in the ring, you must overwrite the WebView webviewclient object.

Mwebview.setwebviewclient (New Webviewclient () {public Boolean shouldoverrideurlloading (WebView view, String URL) { View.loadurl (URL); return true;});
4. If you do not do any processing, browse the Web page, click the System "back" button, the entire Browser will call finish () and end itself, if you want to browse the page fallback rather than launch the browser, you need to process and consume the current Activity Back event.
public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = keyevent.keycode_back) && Mwebview.cangoba CK ()) {mwebview.goback (); return true;} Return Super.onkeydown (KeyCode, event);}
next let's look at how WebView in Android supports Javascripte custom objects, JS has window, history, document and other standard objects in the standard, we can also When developing the browser, we define our object to call the phone system function to handle, so use JS can do whatever you like.
See An example:

public class Webviewdemo extends Activity {private WebView mwebview;private Handler mhandler = new Handler (); @Overrideprotected void on Create (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.webviewdemo); Mwebview = (WebView) Findviewbyid (R.id.webview); WebSettings websettings = Mwebview.getsettings (); websettings.setjavascriptenabled (true); Mwebview.addjavascriptinterface (New Object () {public void clickonandroid () {Mhandler.post (new Runnable () {public void Run () {Mwebview.loadurl ("Javascript:wave ()");}});}, "Demo"); Mwebview.loadurl ("file:///android_asset/demo.html") ;}}
let's look at Addjavascriptinterface (object obj,string InterfaceName), a method that binds a Java object to a JavaScript object, JavaScript The object name is InterfaceName (demo), and the scope is Global. Once the WebView is initialized, the bound Java object can be accessed directly through Javascript:window.demo in the WebView loaded page. Let's see how it's called in HTML.
This makes it possible to invoke the Java object's Clickonandroid () method in JavaScript, and we can define many methods in this object (such as texting, calling a contact list, and so on.), where the Wave () method is called in Java Examples of JavaScript.
Here are a few more things to know:
1) in order for WebView to load the assets,android SDK from the apk file, a schema is provided with the prefix "file:///android_asset/". WebView encountered such a schema, go to the current package in the assets directory to find content. As in the above "file:///android_asset/demo.html".

2) The Java objects and methods to be bound in the Addjavascriptinterface method are run in another thread and cannot be run in the thread that constructs them, which is also the purpose of using Handler.

The use of 2.Android WebView components is detailed
Web content
1, Loadurl Direct display of Web content (separate display network pictures)
2, loaddata display Chinese web content (including the processing of the space)
APK in-Package file
1, Loadurl display APK in the Html and picture files
2, LoadData (loaddatawithbaseurl) display the APK in the image and text mixed Html content
Res/layout/main.xml
XML Code

<? XML version= "1.0" encoding= "Utf-8"? >< linearlayout android:layout_height= "fill_parent" android:layout_width= "Fill_parent" android:orientation= " Vertical "xmlns:android=" http://schemas.android.com/apk/res/android ">< WebView android:layout_height=" Fill_ Parent "android:layout_width=" fill_parent "android:id=" @+id/webview "/></linearlayout><? XML version= "1.0" encoding= "Utf-8"? >< linearlayout android:layout_height= "fill_parent" android:layout_width= "Fill_parent" android:orientation= " Vertical "xmlns:android=" http://schemas.android.com/apk/res/android ">< WebView android:layout_height=" Fill_ Parent "android:layout_width=" fill_parent "android:id=" @+id/webview "/></linearlayout>
Java code

public class Example_webview extends Activity {WebView webview;final string mimeType = "text/html"; final String encoding = "Utf-8";/** called when the activity is first created. */@Overridepublic void onCreate (Bundle savedinstancestate) {super. OnCreate (savedinstancestate); Setcontentview ( R.layout.main); WebView = (WebView) Findviewbyid (R.id.webview); Webview.getsettings (). Setjavascriptenabled (True);////webhtml ();////webimage ();////localhtmlzh ();//// Localhtmlblankspace ();////localhtml ();////localimage ();//localhtmlimage ();} /*** Direct page display */private void webhtml () {try {webview.loadurl ("http://www. google.com");} catch (Exception ex) { Ex.printstacktrace ();}} /*** Direct network picture shows */private void Webimage () {try {webview.loadurl ("http://www. Gstatic.com/codesite/ph/images/code_ Small.png ");} catch (Exception ex) {ex.printstacktrace ();}}  /*** Chinese display */private void Localhtmlzh () {try {String data = "Test Html with Chinese language";//UTF-8 encoding processing (garbled on SDK1.5 emulator and real device, SDK1.6 //webview.loaddata (data, MimeType, encoding);//Encode data (SDK1.5 version) Webview.loaddata (urlencoder. Encode (data, encoding), mimetype,encoding);} catch (Exception ex) {ex.printstacktrace ();}} /*** Chinese display (processing of whitespace) */private void Localhtmlblankspace () {try {String data = "Test Html data with spaces";//do not handle whitespace webview.loaddata (UR Lencoder. Encode (data, encoding), mimetype,encoding);//webview.loaddata (data, mimeType, encoding);//Space processing (in SDK1.5 Version) Webview.loaddata (urlencoder. Encode (data, encoding). ReplaceAll ("\+", ""), MimeType, encoding);} catch (Exception ex) {ex.printstacktrace ();}} /*** display local picture file */private void Localimage () {try {//local file processing (if there is a space in the file name you need to replace with +) Webview.loadurl ("file:///android_asset/ Icon.png ");} catch (Exception ex) {ex.printstacktrace ();}} /*** Displays the local Web page file */private void localhtml () {try {//local file processing (if there are spaces in the file name you need to replace with +) Webview.loadurl ("file:///android_asset/ Test.html ");} catch (Exception ex) {ex.printstacktrace ();}} /*** display local picture and text mixed Html content */private void Localhtmlimage () {try {String data = "Test local picture and text mixed display, this is the image in the APK";//SDK1.5 Local file processing ( Cannot display picture)Webview.loaddata (Urlencoder Encode (data, encoding), mimetype,//encoding);//SDK1.6 and later versions//Webview.loaddata (data, MimeType, encoding);//local file processing (can display picture) Webview.loaddatawithbaseurl ("About:blank", Data, Mimetype,encoding, "");} catch (Exception ex) {ex.printstacktrace ();}}



WebView's introduction is here, if there is an incorrect place, please correct me.

(a) What is Android WebView?

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.