2012-07-12 37 views
2

我有一個活動從數據庫下載數據。雖然活動正在完成這項工作,但我想用ProgressDialog來顯示進度。我使用ProgressDialog.STYLE_HORIZONTAL,因爲我想顯示實際值。我用一個Handler要啓動的顯示ProgressDialog活動:如何在Android中使用intent顯示ProgressDialog?

Intent intent = new Intent(this, ProgressDialogActivity.class); 

private Handler handler = new Handler(){ 
     @Override 
     public void handleMessage(Message msg) { 
      super.handleMessage(msg); 
      if(msg.what == SET_PROGRESS){ 


       intent.putExtra("action", "show"); 
       intent.putExtra("progress", msg.arg1); 
       intent.putExtra("max", msg.arg2); 
       intent.putExtra("message", syncMessage); 
       intent.putExtra("title", R.string.please_wait); 

       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

       startActivity(intent); 
      } 
      else if(msg.what == SHOW_PROGRESS){    


       intent.putExtra("action", "show"); 
       intent.putExtra("title", syncMessage); 

       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

       startActivity(intent);    
      } 
      else if(msg.what == HIDE_PROGRESS){ 


       intent.putExtra("action", "hide"); 
       intent.putExtra("message", ""); 
       intent.putExtra("title", ""); 

       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

       startActivity(intent); 

      } 
     } 
    }; 

這裏是ProgressDialogActivity

public class ScreenProgressDialog extends Activity { 

    ProgressDialog pd; 
    Bundle extras; 
    String action; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     extras = getIntent().getExtras(); 
     pd = new ProgressDialog(this); 

     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pd.setCancelable(false); 

     pd.setProgress(extras.getInt("progress")); 
     pd.setMax(extras.getInt("max")); 
     pd.setMessage(extras.getCharSequence("message")); 
     pd.setTitle(extras.getString("title")); 

     action = extras.getString("action"); 

     if (action.equals("show")){ 
      pd.show();     
     } 
     else{ 
      pd.dismiss(); 
     } 

    } 


    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
    } 

} 

當主要活動從數據庫下載的新表的Handler開始一個新的ProgressDialogActivity並出現一個新的活動。我想避免這一點。我的目標是隻顯示一個活動,其中顯示具有正確值的ProgressDialog。我不能在主要活動中創建ProgressDialog,我必須找到另一種方式,這是某種功課,但我需要一些幫助)。 謝謝!

回答

1

您可以使用AsyncTask在後臺的數據獲取和顯示ProgressDialog 這裏是代碼:

// Get feed! 
new ProgressTask().execute(); 



private class ProgressTask extends AsyncTask<String, Void, Boolean> { 

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this); 

    /** progress dialog to show user that the backup is processing. */ 
    /** application context. */ 

    protected void onPreExecute() { 
     this.dialog.setMessage("Please wait"); 
     this.dialog.show(); 
    } 

    protected Boolean doInBackground(final String... args) { 
     try { 

      /** 
      * Fetch the RSS Feeds from URL 
      */ 
      Utilities.arrayRSS = objRSSFeed 
        .FetchRSSFeeds(Constants.Feed_URL); 
      return true; 
     } catch (Exception e) { 
      Log.e("tag", "error", e); 
      return false; 
     } 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 

     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

     if (success) { 
      // display UI 
      UpdateDisplay(); 
     } 
    } 
} 
1

什麼用AsyncTaskHow to add ProgressDialog
在這裏你可以使用相應的做onProgressUpdatedoInBackground方法下載部分,更新進度條..

+0

我不得不使用意圖... – Alex 2012-07-12 10:31:22

+0

瓦你是否必須使用意圖?你真的應該使用AsyncTask和Progress對話框來做這樣的事情。現在你每次想要更新一個簡單的對話框時都試圖開始一個新的活動。你還想在活動的onPause()方法中關閉對話框,否則你可能會泄漏一個窗口。 – Joel 2012-07-12 19:50:35