android - 如何在 Android 中使用 Intent 显示 ProgressDialog?

标签 android android-intent progressdialog

我有一个从数据库下载数据的 Activity 。当 Activity 执行此工作时,我想使用 ProgressDialog 显示进度。我使用 ProgressDialog.STYLE_HORIZONTAL 因为我想显示实际值。我使用 Handler 启动显示 ProgressDialog 的 Activity:

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

}

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

最佳答案

您可以使用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();
        }
    }
}

关于android - 如何在 Android 中使用 Intent 显示 ProgressDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11447200/

相关文章:

android - ssl23_get_server_hello :tlsv1 alert handshake failure android 4. 4

android - 如何使用 AppCompat v21 在 android 中创建 float 操作按钮 (FAB)?

android - 在 Android 中将 TextView 添加到 LinearLayout

android - 检查 INTENT 互联网连接

android - 在 android 中分享到 Facebook(如 twitter)

android - 动态调用一个类?

android - 在 Android GL 上下文重新加载时显示 ProgressDialog

android - 由于找不到 View ID,项目外部的文件导致构建失败

android - 进度对话框不显示在 onActivityResult 函数上

Android Activity 变暗、失去焦点和卡住 - 没有错误消息并且在首次运行后工作