Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中使用intent显示ProgressDialog?_Android_Android Intent_Progressdialog - Fatal编程技术网

如何在Android中使用intent显示ProgressDialog?

如何在Android中使用intent显示ProgressDialog?,android,android-intent,progressdialog,Android,Android Intent,Progressdialog,我有一个从数据库下载数据的活动。当活动执行此操作时,我希望使用ProgressDialog显示进度。我使用ProgressDialog.STYLE\u HORIZONTAL,因为我希望显示实际值。我使用处理程序启动显示进度对话框的活动: Intent intent = new Intent(this, ProgressDialogActivity.class); private Handler handler = new Handler(){ @Override

我有一个从数据库下载数据的活动。当活动执行此操作时,我希望使用ProgressDialog显示进度。我使用
ProgressDialog.STYLE\u HORIZONTAL
,因为我希望显示实际值。我使用
处理程序启动显示进度对话框的活动

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

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

}
以下是ProgressDialogActivity

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

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

}
当主活动从数据库下载新表时,
处理程序
启动一个新的ProgressDialogActivity,并显示一个新活动。我想避免这种情况。我的目标是只显示一个活动,该活动显示带有正确值的进度对话框。 (我不能在主要活动中创建进度对话框,我必须找到另一种方法。这是一种作业,但我需要一些帮助)。 谢谢大家!

使用:
您可以在
doInBackground
方法中执行下载部分,并使用
onProgressUpdate
相应地更新进度条。

您可以使用在后台获取数据并显示进度对话框 以下是代码:

// 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();
        }
    }
}
//获取提要!
新建ProgressTask().execute();
私有类ProgressTask扩展了AsyncTask{
private ProgressDialog=新建ProgressDialog(HomeActivity.this);
/**“进度”对话框,向用户显示备份正在处理*/
/**应用程序上下文*/
受保护的void onPreExecute(){
this.dialog.setMessage(“请稍候”);
this.dialog.show();
}
受保护的布尔doInBackground(最终字符串…args){
试一试{
/**
*从URL获取RSS源
*/
Utilities.arrayRSS=objRSSFeed
.FetchRSSFeeds(Constants.Feed\u URL);
返回true;
}捕获(例外e){
Log.e(“标记”、“错误”,e);
返回false;
}
}
@凌驾
受保护的void onPostExecute(最终布尔值成功){
if(dialog.isShowing()){
dialog.dismise();
}
如果(成功){
//显示用户界面
UpdateDisplay();
}
}
}

为什么要使用意图?您确实应该使用AsyncTask和Progress对话框来执行类似的操作。现在,每当您想要更新一个简单的对话框时,您都在尝试启动一个新的活动。您还需要关闭活动的onPause()方法中的对话框,否则可能会泄漏窗口。。