如何在等待HttpResponse时显示进度条?

时间:2012-03-28 17:09:36

标签: android webview progress-bar

我是android开发的新手。

我开发了一个Android网络应用程序。当我点击该网页的子菜单或链接时,相关文章是使用XML格式的RSS源从网站上获取的。

此过程需要一些时间来加载页面。我希望在内容加载时显示进度条。

我尝试过以下代码:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
   dialog = ProgressDialog.show(this,"","Loading...");
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);//to remove title bar
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //to remove notification bar
    setContentView(R.layout.main);
    wv = (WebView)findViewById(R.id.webview);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setBackgroundColor(0);
    wv.setWebViewClient(new WebViewClient() {});
    wv.loadUrl("file:///android_asset/HomePage.html");

}
@Override
protected void onSaveInstanceState(Bundle outState)
{
    super.onSaveInstanceState(outState);
    // Save the state of the WebView
    wv.saveState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
    super.onRestoreInstanceState(savedInstanceState);
    // Restore the state of the WebView
     wv.restoreState(savedInstanceState);
}


public class mywebclient extends WebViewClient{

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
        dialog.show();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub
        view.loadUrl("file:///android_asset/HomePage.html");
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
        dialog.dismiss();

    }

}

}

它仅在主页显示进度条,并且在页面完全加载后也没有消失。

从该网页点击子菜单或链接时,如何显示进度条,该怎么办?提前谢谢。

1 个答案:

答案 0 :(得分:3)

试试此代码

package your.app;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Main extends Activity {

WebView webview;

final Activity activity = this;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.main);

    getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
            Window.PROGRESS_VISIBILITY_ON);

    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMessage("Loading...please wait");
    progressDialog.setCancelable(true);

    webview.setWebViewClient(new WebViewClient() {

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

    });

    webview.loadUrl("http://www.mysite.com/mobile/");

    // WebChromeClient give progress etc info
    webview.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView view, int progress) {

            progressDialog.show();
            progressDialog.setProgress(0);
            activity.setProgress(progress * 1000);

            progressDialog.incrementProgressBy(progress);

            if (progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
        }
    });

}

}

注意:别忘了更改您的联系人访问清单文件

此外,您可以添加功能 返回键 检查互联网连接 隐藏标题栏 全屏显示