【问题标题】:How To Make A Countdown Timer Android Java?如何制作倒数计时器 Android Java?
【发布时间】:2014-05-13 09:25:25
【问题描述】:

是否可以使用 Toasts 或 ProgressDialog 制作倒数计时器?

当我启动我的应用程序时,会弹出一个带有文本加载的 ProgressDialog... 在加载数据之前,我会关闭对话框并弹出带有文本视图和内容的列表视图。

但是在启动时我会检查我的网络状态...当网络连接时,我会启动我的后台课程。但如果不是,我会每隔 10 秒使用计时器重新启动我的活动(如下所示)。但是如果没有连接,我会显示一个倒数吐司或更改我的对话框中的文本,例如:(show ->) "Automatic refresh after 10..." () ""Automatic在 9... 之后刷新”直到“0... 之后自动刷新”,然后 10 秒过去了.. 并且带有 finish(); 和 startActivity 的行开始..

这一切都在我的 onCreate 中:

            global.loading_dialog(this); //start progressdialog

        boolean network_connected = false;

    if(check_network.isInternetAvailable(this)) {   
        network_connected = true;
        new connect_task_main().execute(""); //the background class starts
    } else {      
        network_connected = false;
        global.toast.setText("No Internet Connection"); //toast text
        global.toast.show(); //toast start
        global.cancel_toast(2500); //toast stop
    }   

    if (network_connected == false) {
    final Timer timer = new Timer();
            timer.schedule(new TimerTask() {
        public void run() {
            global.spinning_dialog.dismiss(); // when the task activates, then close the dialog
            timer.cancel(); //stop de timer
        finish(); //close the activity
        startActivity(new Intent(main_activity.this, main_activity.class)); //start a new activity
        }
    }, 10000); //after 10 seconds, the task will be active.
} else {
}   

【问题讨论】:

  • @RDC 或许还是帮帮我比较好,尽量给个好答案..?!

标签: java android timer dialog countdown


【解决方案1】:

Android 确实有现成的CountDownTimer

【讨论】:

    【解决方案2】:

    您可以使用Handler 每秒钟调用一次,当它被调用 10 次时,您更改Activity

    做这样的事情:

    final int i = 0;
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
       @Override
       public void run() {
          // Display the time like : textView.setText("Automatic refresh after " + 10 - i +"...");
          if (i == 10)
          {
            global.spinning_dialog.dismiss(); // when the task activates, then close the dialog
            finish(); //close the activity
            startActivity(new Intent(main_activity.this, main_activity.class)); //start a new activity
          }
          else
          {
             i++;
             handler.postDelayed(this, 1000); // Call it 1 second later
          }
       }
    };
    handler.postDelayed(runnable, 0); // Call it immediatly
    

    【讨论】:

      【解决方案3】:

      感谢@giant00 的回答: CountDownCounter

      我更改了部分代码(如下所示):

              if (network_connected == false) {
           new CountDownTimer(12000, 1000) {
               public void onTick(long millisUntilFinished) {
                   global.toast.setText("Automatic Refresh In: " + millisUntilFinished / 1000);
              global.toast.show();
               }
      
               public void onFinish() {
                  finish();
              startActivity(new Intent(main_activity.this, main_activity.class));
               }
            }.start();
          } else {
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多