Skip to content

Instantly share code, notes, and snippets.

@mancdevcarl
Created June 25, 2013 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mancdevcarl/5855364 to your computer and use it in GitHub Desktop.
Save mancdevcarl/5855364 to your computer and use it in GitHub Desktop.
Thread, Runnable, Handler, AsyncTask
public class BlehActivity extends Activity {
private Handler handler;
private ProgressBar progress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = (ProgressBar) findViewById(R.id.progressBar1);
handler = new Handler();
}
public void startProgress(View view) {
// Do something long
Runnable runnable = new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 10; i++) {
final int value = i;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
progress.setProgress(value);
}
});
}
}
};
new Thread(runnable).start();
}
}
//Sequence timed events
class Task1 implements Runnable{
public void run(){
//perform task 1
handler.postDelayed(new Task2(), 2000);
}
}
class Task2 implements Runnable{
public void run(){
//perform task 2
}
}
//now start the first task
handler.post(new Task1());
class Async extends AsyncTask<String, Void, String> {
public Async() {
}
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
return "";
}
@Override
protected void onPostExecute(String result) {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment