Created
June 25, 2013 02:06
-
-
Save mancdevcarl/5855364 to your computer and use it in GitHub Desktop.
Thread, Runnable, Handler, AsyncTask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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