Question

Is it possible to make a countdown timer with Toasts or a ProgressDialog?

When I start my app, there pop-up a ProgressDialog with the text loading... Untill the data is loaded, I dismiss the dialog and the listview pop-up with the textviews and stuff.

But on the startup I check my network status... When the network is connected, I start my background class.. But if it's not, I restart my activity every 10 seconds with a timer (shown below). But I will show a counter down toast or change the text in my dialog if there is no connection, like: (show ->) "Automatic refresh after 10..." (<- hide) (show ->) ""Automatic refresh after 9..." till "Automatic refresh after 0..." and then the 10 seconds are past.. and the lines with finish(); and startActivity begin..

This is all in my 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 {
}   
Était-ce utile?

La solution 3

Thanks to @giant00 with his answer: CountDownCounter

I changed a part of my code (shown below):

        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 {
    }

Autres conseils

Android does have a ready-made CountDownTimer

You can use an Handler to call it each seconds and when it's called 10 times you change the Activity.

Do something like that:

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top