How to monitor WebView display complete event

Source: Internet
Author: User

As we all know, in webview applications, you can use webviewclient to listen to webview content "load" events, such as onpagefinished,onpagestarted and so on. But even when the onpagefinished is triggered, the contents of WebView are not displayed. What do we do when we want to monitor the content of WebView to show the completion of the event?

First, I found the answer online.PictureListener,然后重写onNewPicture方法(http://stackoverflow.com/questions/4065134/is-there-a-listener-for-when-the-webview-displays-its-content)。但是官方文档写的很清楚,这个类已经废弃了(http://developer.android.com/reference/android/webkit/WebView.PictureListener.html#onNewPicture%28android.webkit.WebView,%20android.graphics.Picture%29)。

The

must then use a new method. It is also suggested that you use the invalidate method in the rewrite WebView, and in which you determine the Span class= "pun" >getcontentheight () > 0 To make sure the content has been shown (http://stackoverflow.com/questions/4065134/is-there-a-listener-for-when-the-webview-displays-its-content). This method is similar to the rewrite OnDraw () method. You need to define a listener interface in WebView, such as Onloadfinishlistener , which overrides its onloadfinish () method when it is in use. For example:

public class Articlewebview extends webview{private Onloadfinishlistener monloadfinishlistener;    public interface onloadfinishlistener{public void Onloadfinish ();    } private Boolean isrendered = false;        Public Articlewebview (Context context) {super (context);    Init ();        } public Articlewebview (context context, AttributeSet Attrs) {Super (context, attrs);    Init (); } public Articlewebview (context context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstylea        TTR);    Init ();        } private void Init () {} @Override protected void OnDraw (canvas canvas) {super.ondraw (canvas);            if (!isrendered) {DebugLog.log ("Articlewebview", "Getcontentheight ():" +getcontentheight ());            isrendered = Getcontentheight () >0;            if (monloadfinishlistener!= null) {monloadfinishlistener.onloadfinish (); }}} public void SetonloadfinishlisteNER (Onloadfinishlistener onloadfinishlistener) {this.monloadfinishlistener = Onloadfinishlistener; }}

This seems to be done, but the actual test found that there is still a problem with the wrong trigger time. After troubleshooting, it is found that calling Setonloadfinishlistener () is not the right time, and you must call Setonloadfinishlistener () in the Webviewclient onpagefinished () mentioned earlier. In this way, logic is smooth: The WebView content is loaded, the listener displays the event, and then the display event is triggered.

How to monitor WebView display complete event

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.