2

我有一个从数据库下载数据的活动。当 Activity 进行这项工作时,我想用ProgressDialog显示进度。我使用它ProgressDialog.STYLE_HORIZONTAL是因为我想显示实际值。我使用 aHandler启动显示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();
    }

}

当 Main Activity 从数据库下载一个新表时,会Handler启动一个新的ProgressDialogActivity并出现一个新的 Activity。我想避免这种情况。我的目标是只显示一个显示具有正确值的ProgressDialog的活动。(我无法在主活动中创建ProgressDialog,我必须找到另一种方法。这是某种家庭作业,但我需要一些帮助)。谢谢!

4

2 回答 2

1

使用AsyncTask怎么样:如何添加 ProgressDialog
您可以在哪里执行方法中的下载部分并使用..doInBackground相应地更新进度条onProgressUpdate

于 2012-07-12T07:47:36.437 回答
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();
        }
    }
}
于 2012-07-12T08:25:39.113 回答