我正在尝试添加一个进度条,在应用启动后添加“正在加载...请等待”而不是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);
}
}
答案 0 :(得分:1)
试试这个
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);
}
});
答案 1 :(得分:1)
创建一个方法checkConnection(),它告诉您互联网连接是否可用。这是你如何做到的:
private void checkConnection() {
ConnectivityManager cm = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
isConnected = false;
if (activeNetwork != null && activeNetwork.isConnected()) {
isConnected = true;
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(FeedbackActivity.this);
builder.setTitle("Connection failed");
builder.setMessage("The application without the internet connection may not work. Please check your internet connection.");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
checkConnection();
}
});
builder.show();
}
}
答案 2 :(得分:0)
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
在显示无连接错误的同时使用此功能,因此android会删除进度条。
答案 3 :(得分:0)
因为你有setWebViewClient
两次。但只考虑一个。所以你只需要编写一个setWebViewClient
并将所有覆盖方法(例如pageFinished,onRecievedError)放在其中。