Detailed explanation and examples of Android WebView
Detailed explanation of Android WebView
Android WebView is a special view on Android platform. It can be used to display web pages. This class can be used to display only an online web page in your app, and can also be used to develop a browser.
A high-performance WebKit kernel browser is built into the Android phone and encapsulated into a component called WebView in the SDK. WebKit is a software framework included in Mac OS X V10.3 and above (for v10.2.7 and above, it can also be obtained through software update). At the same time, WebKit is also the basis of safari web browser for Mac OS X. WebKit is an open source project, which is mainly modified from KDE's KHTML and contains some components from apple. Traditionally, WebKit includes a web engine webcore and a scripting engine JavaScript core, which correspond to KHTML and KJS of KDE respectively. However, with the increasing independence of JavaScript engine, WebKit and webcore have been basically mixed (for example, Google Chrome and Maxthon 3 use V8 engine, but still claim to be WebKit kernel).
Several points should be noted in the development process:
1. The license "Android. Permission. Internet" must be used in androidmanifest.xml, otherwise a web page not available error will appear.
2. If there is JavaScript in the visited page, the WebView must be set to support JavaScript.
3. If there is a link in the page, if you want to click the link to continue to respond in the current browser instead of responding to the link in the browser of the newly opened Android system, you must overwrite the webviewclient object of WebView
4. If you browse the web page without any processing, click the "back" button of the system, and the whole browser will call finish() to end itself. If you want the browsed web page to fall back instead of pushing out the browser, you need to process and consume the back event in the current activity.
Next, let's learn how WebView in Android supports JavaScript custom objects. In W3C standard, JS has standard objects such as window, history and document. Similarly, we can define our own objects when developing the browser and call the functions of the mobile phone system to handle them. In this way, we can do whatever we want with JS.
It can be seen that the hyperlink in the HTML code responds to a click event and executes the click () method in JavaScript. Because an object is created through the WebView. Addjavascript interface () method and bound with the JavaScript object, the reference of the JavaScript object is "Android", so onclick () in the newly created object will be called when the click method in html is executed method. This enables the Java code to be invoked in the JavaScript code.
Thank you for reading, hope to help you, thank you for your support to this site!