How to Set the Thread Start Time on an Android
Threads in the Java programming language on the Android platform represent separate lines of execution in a program. Essentially, multiple threads can run in the same program, allowing different lines of execution to occur at the same time in the same program. However, difficulty can arise when trying to schedule threads. Managing when and how Threads execute can prove challenging. While you cannot explicitly give a time to execute a thread, you can specify an object wrapper to delay the execution of a thread through the "sleep" command.
Instructions
-
-
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
}
-
1