Bonjour à tous,

Je me tourne vers vous car après avoir testé tout un tas de code je n'ai point trouvé de solution.

Dans mon application j'ai un compte à rebours (de 20 à 0) pendant lequel l'utilisateur interagie avec l'activity en cours.

J'avais mis au début mon compteur (CountDownTimer) directement dans le code (une fonction appel une classe interne à la classe), ce qui bloquait l'application.

Pour y remédié, j'ai mis la fonction dans un Thread, jusque là l'application fonctionne très bien mais pas moyen de récupérer le temps restant.

Code (simplifié) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
 
/* Entreprise : Emergence IT
 * Développé par : Maxime Nicole
 * Application : Quiz
 * Version : 1.0
 * Date de création : 17/11/2011
 * Date de modification : 18/11/2011
 */
package fr.emergenceit.quiz;
 
import java.util.Timer;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
public class Quiz extends Activity {
 
	public int tempsQuestionSurvival = 20000; // Temps en millisecondes
 
	private String mode1 = "sur";
	private String mode2 = "clm";
 
	public Timer timer;
	public boolean tempsEcoule = false;
	public boolean bonusTempsParDeux = false;
	public Handler handler;
	public Compteur counter;
 
	public int idQuiz;
	public long idPack;
 
	public TextView txtQuestion;
	public TextView txtScore;
	public TextView txtTimer;
	public Button buttonReponse1;
	public Button buttonReponse2;
	public Button buttonReponse3;
	public Button buttonReponse4;
 
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// On affiche le layout menutheme
		setContentView(R.layout.quiz);
 
		// J'exécute mes fonctions
 
		if(modeJeu.equals(mode1)) {
			Log.i("mode", "1");
			mode1();
		}
		else if(modeJeu.equals(mode2)) {
			Log.i("mode", "2");
			mode1();
		}
 
		handler = new Handler() {
			public void handleMessage(Message msg) {
				txtTimer.setText(getString(msg.arg1));
			}
 
		};
	}
 
 
 
	private void mode1() {
		Log.i("mode1", "on entre dans le mode 1");
		if(nbQuestionsFaite<=nbQuestionsSurvival) {
			Log.i("mode1", "On a pas fini le quiz");
 
			// On l'initialise pour dire que l'on a pas répondu à la question
			repondu = false;
 
			// On récupère tous les éléments de notre interface grâce aux ID
			buttonReponse1 = ((Button)this.findViewById(R.id.buttonReponse1));
			buttonReponse2 = ((Button)this.findViewById(R.id.buttonReponse2));
			buttonReponse3 = ((Button)this.findViewById(R.id.buttonReponse3));
			buttonReponse4 = ((Button)this.findViewById(R.id.buttonReponse4));
			txtQuestion = ((TextView)this.findViewById(R.id.txtQuestion));
			txtTimer = ((TextView)this.findViewById(R.id.txtTimer));
 
                        // J'exécute mes fonctions
 
			// On initialise le timer
			if(bonusTempsParDeux) {
				new Thread(new Runnable() { 
					@Override public void run() {
						counter = new Compteur(tempsQuestionSurvival/2, 0);
						// On démarre le compteur
						counter.start();
					}
				});
				Log.i("mode1", "aller on divise le temps par 2");
			}
			else {
				new Thread(new Runnable() { 
					@Override public void run() {
						counter = new Compteur(tempsQuestionSurvival, 0);
						// On démarre le compteur
						counter.start();
					}
				});
				Log.i("mode1", "le temps s'écoule normalement");
			}
                        // J'exécute mes fonctions
 
		}
		else {
			Log.i("mode1", "Il n'y a plus de question");
		}
	}
 
	@SuppressWarnings("unused")
	private void mode2() {
		// TODO Auto-generated method stub
 
	}
 
	private void reponse(int random, int reponse) {
		// J'exécute mes fonctions
 
	}
 
	class Compteur extends CountDownTimer {
		public Compteur(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}
 
		@Override
		public void onFinish() {
			if(!repondu) {
				// J’exécute mes fonctions
 
			}
		}
 
		@Override
		public void onTick(long millisUntilFinished) {
			// TODO là où on affiche le compteur
			// Cette fonction est dans un thread, il faut envoyer les informations
			// vers l'activité
			handler.sendMessage(handler.obtainMessage(1, ""+(millisUntilFinished/1000)));
		}
	}
 
}

Je vous remercie d'avance pour vos réponses.

Cordialement, Maxime.