我如何在android中使用倒计时

时间:2012-03-02 05:54:36

标签: android

我正在使用android中的进度对话框进行加载,但是我想使用倒计时,如果它需要30秒,那么计时器将显示加载像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()

我正在使用此代码。对于msg我完成了消息处理程序。现在我想启动countdown.like计时器。

请帮帮我。

4 个答案:

答案 0 :(得分:1)

您可以使用CountDowntimer类来使用倒数计时器,如下所示

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 :(得分:1)

您可以尝试以下代码,

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 :(得分:0)

您可以使用定时器和写定时器任务更新显示更多定时器http://developer.android.com/reference/java/util/Timer.html

答案 3 :(得分:0)

AlertDialog框可以轻松满足您的要求,您可以在定时器事件后开始循环设置对话文本(如@ the100rabh所示)......

我建议您查看ProgressDialog,因为它们最适合您的要求,这是一个完美的example ...

修改 CountDownTimer可能是最佳选择。正如其中一个答案所建议的......祝你好运!