"Go" JS and Java interaction

Source: Internet
Author: User

How to get content in WebView in Android
Posted on June 13, 2011 by admin
This article summarizes: in the program often uses the WebView to display the webpage, but if can obtain the content in the webpage, this article will give you the simplest case. The end of the article is accompanied by code download.
In the Sina Weibo client needs to use the OAuth authentication, will pop up Sina's certification page, the user in Sina's Web page authorized to return to the program to complete the certification. A URI similar to weibo://oauthactivity is used to return, similar to http://weibo.com. But the UC Browser is unable to complete this address jump, Android comes with a browser is no problem, so I think in the program embedded a webview to display Sina's page to authorize.

The method is very simple, or directly on the code clear some. The first piece of code is very small, just a little bit, and you'll see the back.
Class Handler {
public void Show (String data) {
Toast.maketext (Webviewactivity.this, "performed Handler.show method", 0). Show ();
New Alertdialog.builder (Webviewactivity.this). Setmessage (data). Create (). Show ();
}
}
This is an inside, define a method, the dialog box pops up the content, this is used to display the contents of the WebView.
Here is the key code, you see first, there will be an explanation later.
WebView = new WebView (this);
Setcontentview (WebView);
Webview.loadurl ("http://veikr.com/wap/");
Webview.getsettings (). Setjavascriptenabled (True);
Webview.addjavascriptinterface (New Handler (), "Handler");
Webview.setwebviewclient (New Webviewclient () {
@Override
public void onpagefinished (WebView view, String URL) {
Toast.maketext (webviewactivity.this, "Web page loading complete", 0). Show ();
View.loadurl ("Javascript:window.handler.show (Document.body.innerHTML);");
super.onpagefinished (view, URL);
}
});
1. Webview.getsettings (). Setjavascriptenabled (True);
Set WebView support JS.
2.webview.addjavascriptinterface (New Handler (), "Handler");
Using this code is equivalent to adding a class called handler in the JS of the Web page, and this handler is the inner class that was written earlier. This can be used directly in the Web page: onclick= "javascript:handler.show (' Hello ')"
This means that the Java code is executed directly from the JS in the Web page.
3.webview.setwebviewclient (New Webviewclient ()
Set up a processor for webview (call it this way) and execute the following method after the WebView load is complete
View.loadurl ("Javascript:window.handler.show (Document.body.innerHTML);");
Document.body.innerHTML is a section of JS that gets the contents of the body tag in the Web page and passes this value to the Show method of the handler class. Complete.
You can use the browser to open a Web page, loading completed, in the browser address bar input
Javascript:alert (Document.body.innerHTML);
See what works? is the popup dialog box showing the body content of the Web page?
This is also the use of this truth.
Download code Webviewdemo
Http://veikr.com/201106/android_webview_content-html.html

"Go" JS and Java interaction

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.