2012-03-28 90 views
1

我是android開發的新手。如何在等待HttpRespons時顯示ProgressBar?

我開發了一個android web應用程序。當我點擊子菜單或從該網頁的鏈接時,使用RSS格式的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(); 

    } 

} 

} 

它顯示進度條只在網頁上,它也是後頁面完全加載並沒有解僱。

當從該網頁中點擊子菜單或鏈接時,應該如何顯示進度條?提前致謝。

回答

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(); 
     } 
    }); 

} 

}

注意:不要忘記更改您的清單文件上網

而且u能爲 後退按鈕 檢查互聯網連接 隱藏添加功能標題欄 全屏顯示

+0

非常感謝你bove示例非常好,它解決了我的問題......但還有另外一個問題,其中一個內部頁面包含java腳本,當它被重新寫入該頁面時。此進度條未顯示。有什麼方法可以在頁面重新加載時調用以顯示此進度條? – Ashutosh 2012-03-29 16:31:16

+0

不知道..但是我使用頁面加載腳本在我的HTML – 2012-03-30 07:41:26

+0

當腳本執行時可見的劑量進度條? – Ashutosh 2012-03-30 15:43:13