public void startTask() { //create the task we need to track the timeout for //.... scheduleTimeout(getTimeoutRunnable(), TIMEOUT_MS); } public Runnable getTimeoutRunnable() { return new Runnable() { @Override public void run() { Log.d(TAG, "Timeout - task too more than " + TIMEOUT_MS/1000 + "seconds"); } }; } // called when the task starts public void scheduleTimeout(Runnable timeoutRunnable, long millis) { removeTimeout(); //remove previous timeouts timeoutHandler.postDelayed(timeoutRunnable, millis); } // this is called when the tasks ends either in success or in error public void removeTimeout() { timeoutHandler.removeCallbacksAndMessages(null); }