-1

I need that as a condition the thread use time (sleep) or not.

For example:

Thread splashTread = new Thread() {
              @Override public void run() {
                  try {
                      if (!internet){
                          sleep(4000);
                      }
                          // do loads

                  } catch  (InterruptedException e) { 
                  } finally {
                      startActivity(new Intent( "MainScreen"));
                      finish(); 
                  } 
              } 

When online, the "progressbar" rises properly and visually. When no internet, going too fast and is not visible, I want the user to see the progressbar few seconds in this case. That's why I tried to use sleep, but it does not work if I enter it on one condition.

ephramd
  • 561
  • 2
  • 15
  • 41

2 Answers2

2
Handler handler = new Handler();

Runnable spalshRunnable = new Runnable() {
    @Override
    public void run() {
        startActivity(new Intent(YourActivity.class, MainScreen.this));
        finish(); 
    }
}

inside onCreate:

handler.postDelayed(spalshRunnable, 3000);
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thank you very much @blackbelt Is there any difference in using a thread or handler/runnable? – ephramd Jun 09 '13 at 18:47
  • 1
    all run on the same thread. It is an alternative, imo cleaner – Blackbelt Jun 09 '13 at 18:48
  • 1
    @ephramd for user case you should use handler. also using sleep inside a thread is a bad design http://developer.android.com/training/articles/perf-anr.html – Raghunandan Jun 09 '13 at 18:51
  • 1
    @ephramd you can check the link for reference http://stackoverflow.com/questions/16643177/changing-image-in-imageview-using-threads/16643564#16643564. also check this http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ – Raghunandan Jun 09 '13 at 18:53
0

Ok. Solution:

This thread need two exception: InterruptedException and Exception.

Thread splashTread = new Thread() {
              @Override public void run() {
                  try {
                      if (!internet){
                          sleep(4000);
                      }
                          // do loads

                  } catch  (InterruptedException e) {
                  } catch  (Exception e) {
                  } finally {
                      startActivity(new Intent( "MainScreen"));
                      finish(); 
                  } 
              } 
ephramd
  • 561
  • 2
  • 15
  • 41
  • 1
    If you implement Thread or HandlerThread, be sure that your UI thread does not block while waiting for the worker thread to complete—`do not call Thread.wait() or Thread.sleep()`. use a handler as suggested by black belt – Raghunandan Jun 09 '13 at 18:48