2

I want to make an app that displays some info on the screen, waits a while and then continue to display something else. Is there a simple command to pause an android app? I'm using android 4.2.2.

Frank
  • 2,446
  • 7
  • 33
  • 67
  • 2
    http://stackoverflow.com/questions/4111905/how-do-you-have-the-code-pause-for-a-couple-of-seconds-in-android – Zentoaku Jul 27 '14 at 11:54

3 Answers3

3

You can use Handler class:

Make an instance:

    Handler h=new Handler();

Use your instance to run the postDelayed() method:

   final boolean    postDelayed(Runnable r, long delayMillis)

Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses.

The postDelayed() method has to be feeded with a Runnable containing the code which you want to occur after the delayMillis milliseconds.

The runnable class can be made as following:

    class Runner extends Runnable{ 
         public void run() { 
             //Your codes
         }
    }

As a simple example:

    Handler handler = new Handler(); 
    handler.postDelayed(new Runner(), 2000); 
Aniruddha Sarkar
  • 1,903
  • 2
  • 14
  • 15
1

You can do this by removing the timer ..

Sample code

@Override
public void onClick(View v) {
my_button.setBackgroundResource(R.drawable.icon);
// SLEEP 2 SECONDS HERE ...
Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 
     public void run() { 
          my_button.setBackgroundResource(R.drawable.defaultcard); 
     } 
}, 2000); 
}

Am not an android developer..I recommend you to read this.I got the answer from there ..Hope it helps ..

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26
1

You could use Thread.sleep()

try {
    Thread.sleep(milliseconds);
} catch (InterruptedException e) {
    e.printStackTrace();
}

There might be better ways but this is the one I learnt.