我怎样才能在AsyncTask的添加进度对话框最小加载时间(2秒) [英] How can I add a progress dialog in AsyncTask with minimum loading time (2sec)

查看:211
本文介绍了我怎样才能在AsyncTask的添加进度对话框最小加载时间(2秒)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户点击按钮,将进入b活动从活动A.
然而,由于在活动B,数据将被从互联网上下载的,我想补充一个进度对话框。 有时,连接会非常快,不到一秒钟,有时会超过5秒。
如果进度对话框的展示和酰胺。1秒,我认为这是非常糟糕的用户体验
所以,我想补充一个最小的加载时间,例如2秒。
这意味着,即使装载时间小于2秒,进度对话框也将至少显示2秒。
有没有什么方法可以做到这一点?

When user clicks button, will go to Activity B from Activity A.
However, since in Activity B, data will be downloaded from Internet, I would like to add a progress dialog. Sometimes, the connection will be very fast, less than one second and sometimes will be more than 5 seconds.
If the progress dialog shows <1 sec, I think it is very bad for user experience.
So, I would like to add a minimum loading time, for example, 2 seconds.
That means even the loading time is less than 2 seconds, the progress dialog will also at least show 2 seconds.
Is there any ways to do so?

推荐答案

您可以使用下面code。消磨时间和你的对话这里的对象。

you can use Below Code. Pass Time and Object of your Dialog here.

public void timerDelayRemoveDialog(long time, final Dialog d){
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() {           
        public void run() {                
            d.dismiss();         
        }
    }, time); 
}

EDITED

您可以使用此示例code:

you can Use this Sample Code :

class LoginProgressTask extends AsyncTask<String, Integer, Boolean> {
  @Override
  protected Boolean doInBackground(String... params) {
    try {
      Thread.sleep(4000);  // Do your real work here
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    return Boolean.TRUE;   // Return your real result here
  }
  @Override
  protected void onPreExecute() {
    showDialog(AUTHORIZING_DIALOG);
  }
  @Override
  protected void onPostExecute(Boolean result) {
    // result is the value returned from doInBackground
    removeDialog(AUTHORIZING_DIALOG);
    Intent i = new Intent(HelloAndroid.this, LandingActivity.class);
    startActivity(i);
  }
}

这篇关于我怎样才能在AsyncTask的添加进度对话框最小加载时间(2秒)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆