android simple background thread code example

Example 1: how to run a background thread in android

Thread thread = new Thread() {
  @Override
  public void run() {
      try { Thread.sleep(2000); }
      catch (InterruptedException e) {}

      runOnUiThread(new Runnable() {
          @Override
          public void run() {
              textView.setText("OK");
          }
      });
  }
};
thread.start();

Example 2: thread example in android

//if your project supports java8, you can implement with lambda function
new Thread(() -> {
        // do background stuff here
        runOnUiThread(()->{
            // OnPostExecute stuff here
        });
    }).start();

Tags:

Java Example