Android WebView Use

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/oakpip/archive/2011/04/08/2009800.html

Most of the content for the online finishing other tall posts, now only for finishing, for viewing:

in Andro The ID phone contains a high-performance WebKit kernel browser, encapsulated in the SDK as a WebView component.  
What is Webkit 
WebKit is Mac OS X Software frameworks included in v10.3 and above (for v10.2.7 and above are also available through software updates). At the same time, WebKit is also the foundation of Mac OS X's Safari web browser. WebKit is an open source project, modified primarily by KDE's khtml and contains some components from Apple.  
Traditionally, WebKit contains a Web engine WebCore and a scripting engine JavaScriptCore, They correspond to KDE's khtml and KJS respectively. However, with the increasing independence of the JavaScript engine, 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 an initial experience with a simple example of webview in the SDK's Dev guide when Android is using WebView to browse the Web.  
in the development process should pay attention to several points:      

The 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 Sho                               Uldoverrideurlloading (WebView view, String URL) {view.loadurl (URL);  return  true  ;   }              }); 

< Span style= "LINE-HEIGHT:21PX;" >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 instead of launching the browser, The back event needs to be processed and consumed in the current activity.

 Public boolean onKeyDown (intevent) {               if (keycode = = Keyevent.keycode_back) && mwebview.cangoback ()) {                   mwebview.goback ();                           return true ;               }                return Event );           }     

Next, let's look at how WebView in Android supports Javascripte custom objects, and JS has window,history,document standard objects in the standard. Similarly we can in the development of the browser when we define our own object call phone system function to handle, so use JS can do whatever you like.

See an example:

 Public classWebviewdemo extends Activity {PrivateWebView Mwebview; PrivateHandler Mhandler =NewHandler ();  Public voidonCreate (Bundle icicle) {super.oncreate (icicle);        Setcontentview (R.layout.webviewdemo); Mwebview=(WebView) Findviewbyid (R.id.webview); WebSettings websettings=mwebview.getsettings (); Websettings.setjavascriptenabled (true); Mwebview.addjavascriptinterface (NewObject () { Public voidclickonandroid () {Mhandler.post (NewRunnable () { Public voidrun () {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, and the JavaScript object name is InterfaceName (Demo), 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.

"JavaScript"><!--function Wave () {document.getElementById ("Droid"). src="Android_waving.png"; }       //--></mce:script><body> <a onclick="window.demo.clickOnAndroid ()"> "Droid"Src="Android_normal.png"Mce_src="Android_normal.png"/><br>Click Me! </a> </body> 

This makes it possible to invoke the Java object's Clickonandroid () method in JavaScript, and we can define a number of methods in this object (such as texting, calling the contact list, and so on-phone system functions. Here, the wave () method is an example of invoking JavaScript in Java.

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.

Android WebView Use

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.