我无法停止计时器,尽管我使用条件语句是唯一不会停止重复的操作,但是我不知道将timer.cancel或timer.purgue放在哪里 当Nrepetir = false时,任务不再重复,计时器不会停止,我将尝试清除,取消和task.cancel或有条件的操作,但始终重复。 这里有所有方法:
final Handler handler = new Handler();
final Timer timer = new Timer();
TimerTask task = new TimerTask() {
@Override
public void run() {
if(Nrepetir==true) {
Nrepetir=false;
AsyncHttpClient clienteTurno = new AsyncHttpClient();
RequestParams rpTurno = new RequestParams();
rpTurno.put("IdPartida", IdPartida);
rpTurno.put("IdUsuario", IdUsuario);
clienteTurno.get(urlTurno, rpTurno, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (statusCode == 200) {
String respuesta = response.toString();
Gson gson = new Gson();
ComprobarTurno turno = gson.fromJson(respuesta, ComprobarTurno.class);
int res = turno.getSuccess();
Log.d("DEBUG - MENSAJE TURNO", turno.getMessage());
//Si es correcta
turnoJugador = turno.getPosicion();
turnoActual = turno.getTurno();
valorRecibidoReal = turno.getValorRecibidoReal();
valorRecibido = turno.getValorRecibido();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public boolean getUseSynchronousMode() {
return false;
}
});
AsyncHttpClient clienteCorreos = new AsyncHttpClient();
RequestParams rpCorreos = new RequestParams();
rpCorreos.put("IdPartida", IdPartida);
clienteCorreos.get(urlCorreos, rpCorreos, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (statusCode == 200) {
String respuesta = response.toString();
Gson gson = new Gson();
CorreosPartida correos = gson.fromJson(respuesta, CorreosPartida.class);
int res = correos.getSuccess();
lista.addAll(correos.getCorreos());
Log.d("DEBUG - NOMBRE1", lista.get(0));;
Log.d("DEBUG - NOMBRE2", lista.get(1));;
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public boolean getUseSynchronousMode() {
return false;
}
});
AsyncHttpClient clienteFallos = new AsyncHttpClient();
RequestParams rpFallos = new RequestParams();
rpFallos.put("IdPartida", IdPartida);
clienteFallos.get(urlFallos, rpFallos, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
if (statusCode == 200) {
String respuesta = response.toString();
Gson gson = new Gson();
FallosPartida fallos = gson.fromJson(respuesta, FallosPartida.class);
Log.d("DEBUG - FALLOS", fallos.getMessage());
for (String s : fallos.getFallos()) {
listaFallosVariable.add(Integer.valueOf(s));
Log.d("DEBUG - FALLOS", s);
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
@Override
public boolean getUseSynchronousMode() {
return false;
}
});
}else {
Log.d("DEBUG - TIMER CANCEL", String.valueOf(Nrepetir));
timer.cancel();
}
}
};
Log.d("DEBUG - BOOLEAN", String.valueOf(Nrepetir));
timer.schedule(task, 0, 3000);
答案 0 :(得分:0)
我以前有同样的问题。每当您执行代码时,它都会运行一个新的TimerTask,这就是为什么它将运行与执行次数一样多的原因。为了防止这种情况,我要做的是从数据库中创建另一列,该列检查计划是否已与计时器任务一起进入队列。例如:
运行timerTask后,将状态更改为1,然后在其执行后将其更改为0。如果有条件语句,则只有在状态为0时,timertask才能运行,此条件才有效。
您也可以通过使用全局变量或数组来执行此操作,无论您想要使用哪个,只要您满意即可。