2012-07-12 38 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