I am using asynctask
to execute some task.
I also want to implement to complete in 60 sec else give a time out exception message.
So I am using the AsyncTask.get(time,timeFormat);
Example:
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
validateConnection.execute().get(60, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (TimeoutException e) {
stopConnTask();
invalidCrediantialsError(Utilities.TIMED_OUT_ERROR);
e.printStackTrace();
}catch(CancellationException e){
e.printStackTrace();
};
}
}).start();
It works fine as AsyncTask
. Get blocks in UI
thread so I am running it in separate thread
.
Is this approach right or I have to think of something else.