gpt4 book ai didi

java - Android webview进度条无论有没有网络都会持续加载

转载 作者:行者123 更新时间:2023-12-02 02:49:42 30 4
gpt4 key购买 nike

我试图在应用程序启动后添加一个进度条,上面写着“正在加载...请稍候”,而不是在 webview 应用程序上添加白色页面。我能够做到这一点,但进度条加载不停。即使我断开互联网连接,它仍然会加载而不会停止。如何确保连接后进度条停止并且网站正常加载,否则它不会永远加载,而是告诉我“连接错误。请重试。”

下面是我的代码:

    package com.ibra.ibrawebview;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.support.v7.app.AlertDialog;
import android.content.DialogInterface;

public class MainActivity extends AppCompatActivity {

private WebView webView;
private ProgressDialog progressDialog;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


webView = (WebView) findViewById(R.id.ibraWebView);
webView.getSettings().setJavaScriptEnabled(true);
progressDialog=new ProgressDialog(this);
progressDialog.setMessage("Loading...Please wait");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();

webView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl("http://www.google.com/");
return true;
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});

//give error message if no connection
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
try {
webView.stopLoading();
} catch (Exception e) {
}

if (webView.canGoBack()) {
webView.goBack();
}

webView.loadUrl("about:blank");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Connection Problem");
alertDialog.setMessage("Check your internet connection and try again.");
alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "Try Again", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
startActivity(getIntent());
}
});

alertDialog.show();
super.onReceivedError(webView, errorCode, description, failingUrl);
}
});//end of error message

//begin progress bar


//end of progress bar


}
//goback to previous page
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN){
switch (keyCode){
case KeyEvent.KEYCODE_BACK:
if (webView.canGoBack()){
webView.goBack();
}
else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}



}

最佳答案

试试这个

ProgressBar progressBar;
progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setMax(100);
progressBar.setProgress(1);

webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
});

webView.setWebViewClient(new WebViewClient() {

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);


}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}

@Override
public void onPageFinished(WebView view, String url) {
progressDialog.hide();
progressBar.setVisibility(View.GONE);
}
});

关于java - Android webview进度条无论有没有网络都会持续加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44041225/

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