Android WebView gets the title, content, and image of the page in the method

Source: Internet
Author: User

Title, in Android WebView get the page title, content, image method

The first is to get the title, overriding the Onreceivedtitle () method in New Webchromeclient () {}

@Overridepublic void Onreceivedtitle (WebView view, String title) {super.onreceivedtitle (view, title);//LOGE.E ("__ Page title __ "+title);}

Get the content, is the reference to this side of the

http://blog.csdn.net/hellohaifei/article/details/9331631

First of all except for the most basic ones add a bunch more settings

WebSettings settings = Mwebview.getsettings (); Mwebview.addjavascriptinterface (new Injavascriptlocalobj (), "Local_ Obj ");    Settings.setsupportzoom (TRUE);            Settings.setdomstorageenabled (TRUE);            Mwebview.requestfocus ();            Settings.setsupportzoom (TRUE); Settings.setbuiltinzoomcontrols (TRUE);

And then write a class.

Public final class Injavascriptlocalobj {@JavascriptInterface public void Showsource (String html) {        LOG.E ("", "___" +html+ "____"); }        }

Finally, in the onpagefinished () method of new Webviewclient () {}, add the Call JS method

@Overridepublic void onpagefinished (WebView view, String URL) {//TODO auto-generated method Stubsuper.onpagefinished ( View, URL),/** This is to load the Web content */view.loadurl ("Javascript:window.local_obj.showSource (document.getElementsByTagName (' Article ') [0].innertext);

This method statement and the reference page is a bit different, that is to print all the information, including some tags, and this is just print Web page plain text content.

Later, because of product requirements, share without content, share instructions to directly press, put the URL, but the image to put the first picture, so, you have to change the above sentence

/** This is the */view.loadurl ("Javascript:window.local_obj.showSource (' img ') [0] that loads the first image of the page. SRC); ");

Two needs to be written in addition, specifically do not know

Because the code is confusing, it needs to be explained in Proguard-project.txt.

#webview与js交互需注意 # Preserve annotation, such as @javascriptInterface annotation-keepattributes *annotation* #保留跟 JavaScript-related properties- Keepattributes javascriptinterface# Preserve the method in Javascriptinterface-keepclassmembers class * {@ Android.webkit.JavascriptInterface <methods>;} #这个根据自己的project来设置, this class is used to interact with JS, so the fields in this class, methods, and so on try to keep-keepclassmembers public class   com.tugouzhong.activity.webviewactivity{<fields>;   <methods>; public *;} #这个类 must be preserved, this class passes data in Wvjbwebviewclient, and if confused causes some callback not to call-keep class com.packgename.custom.wvjbwebviewclient$ The variable names of members in the wvjbmessage# class cannot be confused, and these variable names are used as fields in JSON and cannot be changed. -keepclassmembers class com.tugouzhong.activity.webviewactivity$injavascriptlocalobj{<fields>;}

Found on the Internet, measured no problem, first together

WebView get the title, content, and image of the page in Android

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.