В файле класса активности:
private boolean status = false; //set a initial flag
private Handler customHandler;
customHandler = new Handler();
Runnable currentThreadTask = new Runnable() {
@Override
public void run() {
status = customfunction();
//call your function,suppose the function executed successfully,'status' changed to ture.
}
};
customHandler.post(currentThreadTask);
public boolean customfunction(){
//Time consuming operations
return true;
}
И создайте countDownTimer, например:
new CountDownTimer(5000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {//5 seconds passed
//Check 'status' value
//If status is still equal false,then customfunction executed failed in 5 seconds
//otherwise it successed.
}
}.start();