Skip to content

Instantly share code, notes, and snippets.

@marcoRS
Created June 25, 2015 20:50
Show Gist options
  • Save marcoRS/3f6f469b291183690e94 to your computer and use it in GitHub Desktop.
Save marcoRS/3f6f469b291183690e94 to your computer and use it in GitHub Desktop.
Background Thread
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
/** You can also choose a cached thread pool to run threads in parallel. */
public static final Executor sExecutor = Executors.newSingleThreadExecutor();
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
}
@Override protected void onStop() {
super.onStop();
sExecutor.execute(new Runnable() {
@Override public void run() {
Log.i("BackgroundThread", "I am sleeping");
SystemClock.sleep(30000);
Log.i("BackgroundThread", "I am returned");
}
});
}
@Override protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "I was destroyed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment