1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
public class TestActivity extends Activity {
...
public void onCreate(Bundle savedInstanceState) {
//Code executé dans le thread principal
super.onCreate(savedInstanceState);
...
//On lie une action au bouton
unButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Création et exécution d'un nouveau thread
new Thread(new Runnable() {
@Override
public void run() {
// Instructions consommatrices en temps
// Et pour finir, déposer un Runnable dans l'UI Thread
post(new Runnable(){
//Executé par l'UI thread
//Ex: modifier un TextView
});
}
}).start();
}
}
...
} |
Partager