Adding a delay

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

Adding a delay

Postby azhar » Tue May 17, 2011 8:05 am

Hi,
I have been trying to add some delay in my application, but unable to do so, I used the Thread.sleep(5000) method to get some delay, but not working as expected.
When i add sleep(5000) method at 2 places with some functionality between them, the application goes to sleep for 10 seconds and then the entire functionality executes.

Can someone please help regarding this?

Regards.
azhar
Freshman
Freshman
 
Posts: 3
Joined: Mon Mar 21, 2011 11:41 am

Top

Re: Adding a delay

Postby Scott Longley » Tue May 17, 2011 2:36 pm

You're not giving your UI thread any breathing room, everything will run (and in this case, sleep) on one thread, unless you specify.

This is probably what you want, I just tested it 5 minutes ago:

Let's initialise it, first.
Code: Select all
private Thread thread;


Chuck this in where you need it.
Code: Select all
thread = new Thread() {
   public void run () {
      try {
          sleep(5000);
      } catch (InterruptedException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
                   
      // Do what you needed to do here
    }
};
thread.start();


Please reply if this doesn't work or help you. Good luck! :)
Scott Longley
Freshman
Freshman
 
Posts: 2
Joined: Tue May 17, 2011 1:49 pm

Re: Adding a delay

Postby azhar » Tue May 24, 2011 12:09 pm

Thanks for ur reply, i tried ur code nd the results are same as mentioned above.........

Code: Select all
System.out.println("Thread before sleep1");
thread = new Thread() {
             public void run () {
                try {
                    sleep(5000);
//                    word1.setTextColor(Color.RED);
                    System.out.println("Thread after sleep1");
                } catch (InterruptedException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
                }
                             
                // Do what you needed to do here
              }
          };
          thread.start();
          
           System.out.println("Thread before sleep2");
           thread = new Thread() {
                 public void run () {
                    try {
                        sleep(5000);
//                        word1.setTextColor(Color.RED);
                        System.out.println("Thread after sleep2");
                    } catch (InterruptedException e) {
                       // TODO Auto-generated catch block
                       e.printStackTrace();
                    }
                                 
                    // Do what you needed to do here
                  }
              };
              thread.start();
              
               System.out.println("Thread before sleep3");
              thread = new Thread() {
                    public void run () {
                       try {
                           sleep(5000);
//                           word1.setTextColor(Color.RED);
                           System.out.println("Thread after sleep3");
                       } catch (InterruptedException e) {
                          // TODO Auto-generated catch block
                          e.printStackTrace();
                       }
                                    
                       // Do what you needed to do here
                     }
                 };
                 thread.start();


and the output that i got in the logcat was............

Code: Select all
Thread before sleep1
Thread before sleep2
Thread before sleep3
(and after a delay of 15 secs)
Thread after sleep1
Thread after sleep2
Thread after sleep3


whereas the desired output shud have been..........
Code: Select all
Thread before sleep1
(and after a delay of 5 secs)
Thread after sleep1

Thread before sleep2
(and after a delay of 5 secs)
Thread after sleep2

Thread before sleep3
(and after a delay of 5 secs)
Thread after sleep3



Can u help with my requirement?
azhar
Freshman
Freshman
 
Posts: 3
Joined: Mon Mar 21, 2011 11:41 am

Top

Return to General

Who is online

Users browsing this forum: No registered users and 7 guests