Summary of webview usage in Android-the foundation for rapid browser implementation.

Source: Internet
Author: User

My webview trainer Project

Fast browser http://bbs.ifeisu.com/

 

Coming soon

Latest interface:

Http://blog.csdn.net/gumanren/article/details/7010316

Webview is used in all aspects.

Multi-tag browsing does not look like chrome?

Convenient Internet access (voice-based Internet access, and frequent web sites are automatically prompted)

Various homepage styles

 


Therefore, some common problems of webview are specially collected here,

Most of the content is sorted out by other senior posts on the Internet, which are only used for viewing:

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.

========================================================== ========================================
 
 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 do not do any processing, browse the Web page and click the system "back" key, the entire browser will call finish () to end itself. If you want to browse the Web page back rather than launch the browser, the back event needs to be processed and consumed in the current activity.

Public Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back) & mwebview. cangoback () {mwebview. goback (); Return true;} return Super. onkeydown (keycode, event );}

5. webview in Android supports JavaScript custom objects

A. Set webview to support Javascript. websettings. setjavascriptenabled (true );

B. Bind the android object to the JavaScript Object. addjavascriptinterface (Object OBJ, string interfacename );

C. The page calls the JavaScript Object. javascript: window. Demo. Method Name ();

In W3C standards, JS has standard objects such as window, history, and document. Similarly, we can define our own objects when developing browsers to call mobile phone system functions for processing, in this way, you can do whatever you want.

View an instance:

Public class webviewdemo extends activity {private webview mwebview; private handler mhandler = new handler (); Public void oncreate (bundle icicle) {super. oncreate (icicle); setcontentview (R. layout. webviewdemo); mwebview = (webview) findviewbyid (R. id. webview); 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 ");}}

The addjavascriptinterface (Object OBJ, string interfacename) method binds a Java object to a JavaScript Object. The JavaScript Object Name Is interfacename (DEMO) and the scope is global. After webview initialization, you can directly access the bound Java object through javascript: window. demo on the page loaded by webview.

Let's see how html is called.

<HTML>
<Script language = "JavaScript"> <! --

Function wave (){
Document. getelementbyid ("droid"). src = "android_waving.png ";
}

// --> </SCRIPT>
<Body>
<A onclick = "window. Demo. clickonandroid ()">
<br>
Click me!
</A>
</Body>
</Html>

In this way, you can call the clickonandroid () method of the Java object in Javascript. We can also define many methods in this object (such as sending text messages and calling the contact list and other mobile phone system functions .), Here, the wave () method is an example of calling Javascript in Java.

Here are some knowledge points:

1) To allow webview to load assets from the APK file, the android SDK provides a schema prefixed with "file: // android_asset /". When webview encounters such a schema, it finds the content in the Assets Directory of the current package. Above "file: // android_asset/demo.html"
2) the Java object and method to be bound in the addjavascriptinterface method must run in another thread and cannot run in the construction of another thread. This is also the purpose of using handler.


6. When the DIV layer is used in webview, overlapping displays occur.

You can only use Dom to delete a div.

7.In Android, when webview receives favicon, it always gets a null solution.

8. Obtain the history list in 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.