In my android app I am having a thread in which I fetch data from a web service.
So normally it works well, but sometimes if the connection is too slow it kind of hangs.
So is there any way by which I can set some time say 1 min, and if the thread process is not
completed in 1 min. then I would like to stop this thread and display a message to the user that connection is weak/slow and try later.
Please help !!
In my android app I am having a thread in which I fetch data
Share
I don’t know whether it is supported in Android, but this is exactly what the Future objects returned from an ExecutorService are supposed to do for you. In particular, the cancel(boolean) method can be used to interrupt the task if it has started but not finished.
The tasks should be written to be aware that they may be interrupted, and abort cleanly if they have been. Most of the framework IO methods can be interrupted, so you just need to worry about your own code.