等待线程完成时显示进度对话框 2 秒

发布于 2024-11-04 11:13:49 字数 1413 浏览 0 评论 0原文

我有一个活动,其中我调用一个可能需要一段时间才能完成的服务,并且在该服务未完成之前,单击的菜单选项应该返回一条错误消息,例如“数据尚未准备好,请尽快重试” 但是,我想在抛出该错误之前给它几秒钟的时间来完成,在此期间,我想在屏幕上显示一个进度对话框。 这是我的代码:

if(calculatedValue.equals(NOT_YET_CALCULATED)){
                //show progress dialog
                ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
                long timeStarted = System.currentTimeMillis();
                while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                    // wait for at most 1.5 seconds
                }
                progress.dismiss();
                if(calculatedValue.equals(NOT_YET_CALCULATED)){
                    //if it's still not there, there's probably a problem, show generic alert dialog
                    AlertDialog dialog = new AlertDialog.Builder(this).create();
                    dialog.setTitle(R.string.not_yet_calulated_alert_title);
                    dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
                    dialog.show();
                }else{
                    startActivity(j);
                }
            }else{
                startActivity(j);
            }

让我解释一下: 我检查该值是否存在,如果不存在,我会显示一个进度图标(我要寻找圆圈)1.5 秒。如果在那之后它仍然不存在,我会放弃并显示一条错误消息。

问题是进度内容没有显示在屏幕上。 我做错了什么?

谢谢, e.

I have an activity in which I call a service that can take a while to complete and until that service didn't finish, the menu options that are clicked should return an error message like "data not yet ready, please try again soon"
however, I want to give it a couple of seconds to finish before I throw that error and during that time, I want to show a progressdialog on the screen.
here is the code that I have:

if(calculatedValue.equals(NOT_YET_CALCULATED)){
                //show progress dialog
                ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
                long timeStarted = System.currentTimeMillis();
                while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                    // wait for at most 1.5 seconds
                }
                progress.dismiss();
                if(calculatedValue.equals(NOT_YET_CALCULATED)){
                    //if it's still not there, there's probably a problem, show generic alert dialog
                    AlertDialog dialog = new AlertDialog.Builder(this).create();
                    dialog.setTitle(R.string.not_yet_calulated_alert_title);
                    dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
                    dialog.show();
                }else{
                    startActivity(j);
                }
            }else{
                startActivity(j);
            }

Let me explain:
I check if the value exists, and if it doesn't I show a progress icon (i'm going for the circle thingy) for 1.5 seconds. If after that time it's still not there, I give up and show an error message.

The problem is that the progress thing does not show up on the screen.
what am I doing wrong?

thanks,
e.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2024-11-11 11:13:49

想通了,应该为此使用异步任务,代码如下:

    if(calculatedValue.equals(NOT_YET_CALCULATED)){
    //show progress dialog
    final ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
    AsyncTask<Void, Void, Boolean> waitForCompletion = new AsyncTask<Void, Void, Boolean>(){
        @Override
        protected Boolean doInBackground(Void... params) {
            long timeStarted = System.currentTimeMillis();
            while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                // wait for 1.5 ms
                 try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        Log.e(TAG, "thread interrupted", e);
                    }
            }
            progress.dismiss();
            return calculatedValue.equals(NOT_YET_CALCULATED);
        };
        @Override
        protected void onPostExecute(Boolean result) {
        if(result == true){
            AlertDialog dialog = new AlertDialog.Builder(TodayActivity.this).create();
            dialog.setTitle(R.string.not_yet_calulated_alert_title);
            dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
            dialog.show();
        }else{
            i.putExtra(Constants.AMOUNT, Integer.parseInt(calculatedValue));
            startActivity(i);
        }
        }
    };
    waitForCompletion.execute(null, null, null);
}else{
i.putExtra(Constants.AMOUNT, Integer.parseInt(calculatedValue));
startActivity(i);
}

figured it out, should use async task for this, here's the code:

    if(calculatedValue.equals(NOT_YET_CALCULATED)){
    //show progress dialog
    final ProgressDialog progress = ProgressDialog.show(this, "", getResources().getString(R.string.wait), true);
    AsyncTask<Void, Void, Boolean> waitForCompletion = new AsyncTask<Void, Void, Boolean>(){
        @Override
        protected Boolean doInBackground(Void... params) {
            long timeStarted = System.currentTimeMillis();
            while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                // wait for 1.5 ms
                 try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        Log.e(TAG, "thread interrupted", e);
                    }
            }
            progress.dismiss();
            return calculatedValue.equals(NOT_YET_CALCULATED);
        };
        @Override
        protected void onPostExecute(Boolean result) {
        if(result == true){
            AlertDialog dialog = new AlertDialog.Builder(TodayActivity.this).create();
            dialog.setTitle(R.string.not_yet_calulated_alert_title);
            dialog.setMessage(getResources().getString(R.string.not_yet_calulated_alert_message));
            dialog.show();
        }else{
            i.putExtra(Constants.AMOUNT, Integer.parseInt(calculatedValue));
            startActivity(i);
        }
        }
    };
    waitForCompletion.execute(null, null, null);
}else{
i.putExtra(Constants.AMOUNT, Integer.parseInt(calculatedValue));
startActivity(i);
}
ㄖ落Θ余辉 2024-11-11 11:13:49

你现在所做的至少会导致僵局。

我通常做的是创建进度对话框,就像您现在所做的那样,同时启动另一个线程,在这种情况下等待 1.5 秒,然后停止进度对话框。

例如

    private Runnable waitforcompletion = new Runnable()
{
    @Override
    public void run()
    {
         while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                Thread.sleep(10);
                }

                progress.dismiss(); 
    }
};

,为了开始,只需在创建进度对话框后调用上面的可运行程序

What you are making now atleast causes a deadlock.

What I normally do is creating the progressdialog as you are doing now and on the same time start another thread that in this situation waits for 1.5second and stop the progressdialog then.

For example

    private Runnable waitforcompletion = new Runnable()
{
    @Override
    public void run()
    {
         while(calculatedValue.equals(NOT_YET_CALCULATED) && System.currentTimeMillis() - timeStarted < 1500){
                Thread.sleep(10);
                }

                progress.dismiss(); 
    }
};

And for starting just call the above runnable after creating the progressdialog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文