Wednesday, November 28, 2012

Start Thread On Android

You can start a new thread by extending the Thread class or implementing the Runnable interface.

Extending the Thread class:
Thread T = new NewThread();
T.start();

private class NewThread extends Thread {
  @Override
  public void run() {
    //Your asynchronous code
  }
}
Implementing the Runnable interface:
Thread T = new Thread(new NewRunnable());
T.start();

private class NewRunnable implements Runnable {
  public void run() {
    //Your asynchronous code
  }
}
Your activity may implement the Runnable interface directly.
In this case, when creating the Thread object you must pass the activity (this) as the parameter.

No comments:

Post a Comment