Thứ Tư, 30 tháng 1, 2013
android book2
When you integrate a WebView into your activity, you can control what Web
pages are displayed, whether they are from a local provider or come from
over the Internet, what should happen when a link is clicked, and so forth.
And between WebView, WebViewClient, and WebSettings, you can control a fair
bit about how the embedded browser behaves. Yet, by default, the browser
itself is just a browser, capable of showing Web pages and interacting with
Web sites, but otherwise gaining nothing from being hosted by an Android
application.
Except for one thing: addJavascriptInterface().
The addJavascriptInterface() method on WebView allows you to inject a Java
object into the WebView, exposing its methods, so they can be called by
Javascript loaded by the Web content in the WebView itself.
Now you have the power to provide access to a wide range of Android
features and capabilities to your WebView-hosted content. If you can access it
from your activity, and if you can wrap it in something convenient for use
by Javascript, your Web pages can access it as well.
For example, Google's Gears project offers a Geolocation API, so Web pages
loaded in a Gears-enabled browser can find out where the browser is
located. This information could be used for everything from fine-tuning a
search to emphasize local content to serving up locale-tailored advertising.
We can do much of the same thing with Android and
addJavascriptInterface().
In the WebView/GeoWeb1 project, you will find a fairly simple layout
(main.xml):
Đăng ký:
Đăng Nhận xét (Atom)
Không có nhận xét nào:
Đăng nhận xét