Friday, March 7, 2014

How to Set the Thread Start Time on an Android



1. Create a basic class to contain the thread:

class Example implements Runnable{}

2. Define the 'run' method of the class. This is the main method of a thread class, and will execute during an Android event:

class Example implements Runnable{public void run(){

}

}

3. Define the code in the thread. What the code does will vary according to your needs, but to control its execution time, begin the 'run' method with a call to the 'sleep' command, which will pause thread execution for x seconds:

class Example implements Run{public void run(int x){

Thread.sleep(x)//sleep for x seconds

/*other code*/

}

}

4. Create an object from class 'Example' during an Android event in code:

public void onClick(View v){

Example e = new Example;

e.start(20000); //waits for 20 seconds

}

No comments:

Post a Comment