2

In my android app, I'm using a method that tries to get the user's current location in a custom java class. I would like to set a timeout that, when it expires, can do whatever I want. I also want that timeout to be cancelable.

How can I do this in a simple way?

Thanks

Romain Pellerin
  • 2,470
  • 3
  • 27
  • 36

1 Answers1

3

You can do this in many ways

TimerTask

CountDownTimer

Thread.sleep() (on a background thread, please)

Runnable

You have many options, it just depends on how you want to implement it and what you need. There are many examples and tutorials for each on The Google and SO.

CountDownTimer sounds pretty good for this because you can set the time in millis and do what you want in onFinish() but any could work.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Just read the documentation and CountDownTimer sould be a good solution. Does it run a another thread that the UI thread? – Romain Pellerin Aug 15 '13 at 21:55
  • 1
    No, that's why you can update the `UI`. But it doesn't halt the `UI` – codeMagic Aug 15 '13 at 21:57
  • Maybe I should consider using Handler+Runnable? – Romain Pellerin Aug 15 '13 at 22:02
  • If you want your timer to run in the background then that may be better. I have no idea what you're doing so I can't say for sure but that will probably be safe for you – codeMagic Aug 15 '13 at 22:03
  • I've created my own 'MyLocationManager' class which tries to retrieve the use's location using new Google Play Services API. But sometimes I takes too much time because of a bad network so I wanna set a timeout to cancel everything. In your opinion, does it need to be executed on the main UI or another? Thanks again for your help – Romain Pellerin Aug 15 '13 at 22:09
  • I would do it on the background `Thread` and set a flag to true when the location is found. Then you can do a `Thread.sleep(millis)` with a `while` loop. `while (!flag || timeNotUp){ Thread.sleep(millis); millis += millis + incrementTime` Or something similar – codeMagic Aug 15 '13 at 22:12
  • Sorry to ask this but I'm not not comfortable with threads, could you please provide me a full example? What about Handler? – Romain Pellerin Aug 15 '13 at 22:19
  • I typically use `AsyncTask` because I think its easy to implement once you understand how it works and it will allow you to update the `UI` easily. [Here is an answer of mine](http://stackoverflow.com/questions/15377326/how-to-do-functions-in-an-asynctask/15377400#15377400) that shows the basic structure – codeMagic Aug 15 '13 at 22:23
  • `Handler` would be fine also, just search on SO for handler example and you should get a lot of results – codeMagic Aug 15 '13 at 22:24
  • Yes, I already know AsyncTask but here that doesn't fit what I need. I'm gonna switch to handler+runnable I think, thank you – Romain Pellerin Aug 15 '13 at 22:28
  • 1
    `doInBackground()` runs on a background `Thread` so you can call `Thread.sleep()` there and update `UI` in any other of its methods. But `Handler` may work better for you. You're welcome – codeMagic Aug 15 '13 at 22:29