如何在android中使用倒计时

I am using progress dialog box in android for loader but I want to use count down like if it takes 30 sec then the timer will be shown with loading like 30-29-28-27.

Dialog = new ProgressDialog(myactivity.this);
Dialog.setMessage("Data is syncronising it may take....30sec");
Dialog.show();
new Thread() {
public void run()

I am using this code.and for msg I am finish message handler.Now I want to start countdown.like timer.

Please help me.

#0

You can use countdown timer by using CountDowntimer class as below

import android.os.CountDownTimer;

MyCount count=new MyCount(totalduration in milliseconds,pertick duration in milliseconds);  

public class MyCount extends CountDownTimer{

                public MyCount(long millisInFuture, long countDownInterval) {
                   super(millisInFuture, countDownInterval);
                   }

                   @Override
                   public void onFinish() {


                    }

                   @Override
                   public void onTick(long millisUntilFinished) {



                     }
            }

#1

You can try following code,

public class ProgressBarExampleActivity extends Activity 
{
    ProgressThread progThread;
    ProgressDialog progDialog;
    Button button;
    int typeBar;
    int delay = 1000;                  // Milliseconds of delay in the update loop
    int maxBarValue = 30;           // Maximum value of horizontal progress bar

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.Button);
        button.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                typeBar = 1;
                showDialog(typeBar);
            }
        }); 
    }

    @Override
    protected Dialog onCreateDialog(int id) 
    {
        switch(id) 
        {
        case 1:                      // Horizontal
            progDialog = new ProgressDialog(this);
            progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progDialog.setMax(maxBarValue);
            progDialog.setMessage("Dollars in checking account:");
            progThread = new ProgressThread(handler);
            progThread.start();
            return progDialog;
        default:
            return null;
        }
    }

    final Handler handler = new Handler() 
    {
        public void handleMessage(Message msg) 
        {
            // Get the current value of the variable total from the message data
            // and update the progress bar.
            int total = msg.getData().getInt("total");
            progDialog.setProgress(total);
//          if (total >= maxBarValue)
            if (total <= 0 )            
            {
                dismissDialog(typeBar);
                progThread.setState(ProgressThread.DONE);
            }
        }
    };

    private class ProgressThread extends Thread 
    {   
        // Class constants defining state of the thread
        final static int DONE = 0;
        final static int RUNNING = 1;

        Handler mHandler;
        int mState;
        int total;

        ProgressThread(Handler h) 
        {
            mHandler = h;
        }

        @Override
        public void run() 
        {
            mState = RUNNING;   
            total = maxBarValue;
            while (mState == RUNNING) 
            {
                // The method Thread.sleep throws an InterruptedException if Thread.interrupt() 
                // were to be issued while thread is sleeping; the exception must be caught.
                try 
                {
                    // Control speed of update (but precision of delay not guaranteed)
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    Log.e("ERROR", "Thread was Interrupted");
                }

                // Send message (with current value of  total as data) to Handler on UI thread
                // so that it can update the progress bar.

                Message msg = mHandler.obtainMessage();
                Bundle b = new Bundle();
                b.putInt("total", total);
                msg.setData(b);
                mHandler.sendMessage(msg);

                total--;    // Count down
            }
        }

        // Set current state of thread (use state=ProgressThread.DONE to stop thread)
        public void setState(int state) 
        {
            mState = state;
        }
    }

}

#2

You can make use of Timer and write timer task to update display More on Timer at http://developer.android.com/reference/java/util/Timer.html

#3

Your requirement can be fulfilled easily by AlertDialog boxes, in which you can start a loop to set the text of dialog after the timer event (as suggested by @the100rabh) ...

Howver I will suggest that you have a look on ProgressDialog too since they are best for your requirement, here is a perfect example of it...

EDIT CountDownTimer can be the best choice. As suggested in one of the answer.... Good Luck!

推荐文章

什么时候开始使用string replace而不是sprintf?

什么时候开始使用string replace而不是sprintf?

推荐文章

在Objective-C中,是否有在.m文件中声明全局变量的特定规则?

在Objective-C中,是否有在.m文件中声明全局变量的特定规则?

推荐文章

npm不适用于node v0.4.12mac

npm不适用于node v0.4.12mac

推荐文章

我怎么做svn:外部工作使用Xcode 4?

我怎么做svn:外部工作使用Xcode 4?

推荐文章

wxWidgets对话框没有最大化框

wxWidgets对话框没有最大化框

推荐文章

当需求以这种方式发展时,如何分离责任?

当需求以这种方式发展时,如何分离责任?

推荐文章

eclipsejsp:move.tag公共导入到共享文件?

eclipsejsp:move.tag公共导入到共享文件?

推荐文章

Phonegap-Android后退按钮-如何捕捉按住事件

Phonegap-Android后退按钮-如何捕捉按住事件

推荐文章

使用类型.GetType(字符串),但找不到类型或命名空间名称“t”

使用类型.GetType(字符串),但找不到类型或命名空间名称“t”

推荐文章

vs2010混乱中的sql调试

vs2010混乱中的sql调试

推荐文章

自动添加到用户列表

自动添加到用户列表

推荐文章

多维向量超立方体

多维向量超立方体

推荐文章

获取PHP文件内容上传错误

获取PHP文件内容上传错误

推荐文章

如何在中从sqlserver数据库导出数据到excel2007ASP.NET?

如何在中从sqlserver数据库导出数据到excel2007ASP.NET?

推荐文章

在android中比较字符串数组和一个字符串?

在android中比较字符串数组和一个字符串?

推荐文章

如何扩展QClipboard?

如何扩展QClipboard?