gpt4 book ai didi

android - 如何在 webview 中成功加载网页时触发功能?

转载 作者:太空狗 更新时间:2023-10-29 12:49:51 26 4
gpt4 key购买 nike

我想在 webview 完成加载网页并且没有收到错误后触发一个函数。根据我的实验,onPageFinished() 会被触发多次,并且在大多数情况下,它会在 onReceivedError() 之前执行。

那么我怎么知道网页已经加载并且没有收到错误呢?

最佳答案

您可以在 WebView 中设置 WebViewClient 并覆盖其回调方法,这样当网页加载完成时,您就会知道。例如

webView.setWebViewClient(new MyWebViewClient());


private class MyWebViewClient extends WebViewClient {


@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
if(progess!=null){
progess.dismiss();

}
progess = ProgressDialog.show(LoadUrl.this, "", "Loading please wait...");
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.shouldOverrideUrlLoading(view, url);
if (Uri.parse(url).getHost().equals("callback")) {
// This is my web site, so do not override; let my WebView load the page
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs

view.loadUrl(url);
return true;
}

@Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view, url);

// Add YOUR CODE HERE
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Toast.makeText(LoadUrl.this, "Sorry! " + description, Toast.LENGTH_SHORT).show();
}

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
super.onReceivedSslError(view, handler, error);
handler.proceed();


}
}

此处 onPageFinished(WebView view, String url) 将在 webView 完成加载当前页面时调用。

希望对您有所帮助:)

关于android - 如何在 webview 中成功加载网页时触发功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13377836/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com