好吧,我有一个线程在我的倒数计时器启动之前运行,然后当该线程中的变量更改其值时,该变量将触发onTick()
方法中的条件,该方法将调用onFinish()
并执行所需的操作,问题是在onTick()
方法上应用条件,倒数计时器将继续运行并执行onFinish()
方法不止一次,我尝试使用自定义倒数计时器,该计时器允许我取消onTick()
内部的计时器,但是它不起作用,应用程序只是挂起,如果onTick()
已经执行了onFinish()
,我也试图添加一个标志来执行一次代码,但是它不起作用,它返回假值,有时返回到父计时器PS。我确实有两个倒数计时器,我想将其应用于上述过程;是存在于第一个对象的onFinish()
中的第二个对象,代码如下:
cdt= new CountDownTimer(10000,1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
//here you can have your logic to set text to edittext
}
public void onFinish() {
if(connectionAccepted!=null){
new CountDownTimer(15000,1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {if(assignMsg!=null){
onFinish();
//Stop the timer here so it doesn't call onFinish more than once
}
//here you can have your logic to set text to edittext
}
public void onFinish() {
chatClient.interrupt();
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
socket.close();
progressDialog.dismiss();
if(assignMsg!=null){
new AlertDialog.Builder(TicketDetails.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Result")
.setMessage("Task has been: "+assignMsg)
.setPositiveButton("Ok", null)
.show();
}else{
chatClient.interrupt();
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
socket.close();
new AlertDialog.Builder(TicketDetails.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Result")
.setMessage("User didn't respond!")
.setPositiveButton("Ok", null)
.show();
}
}
}
.start();
}else{
chatClient.interrupt();
try {
serverSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
socket.close();
progressDialog.dismiss();
new AlertDialog.Builder(TicketDetails.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Result")
.setMessage("Network Error!")
.setPositiveButton("Ok", null)
.show();
}
}
}
.start();
}});
任何帮助表示赞赏。
最佳答案
似乎无法在onFinish()
内部调用onTick()
,这是您可以执行的操作
@Override
public void onTick(long millisUntilFinished){
//your code here
new Handler().post(new Runnable(){
@Override
public void run(){
onFinish();
}
});
}