Romain Pellerin
Romain Pellerin

Reputation: 2480

Custom timeout for a method - Android

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

Upvotes: 2

Views: 16790

Answers (1)

codeMagic
codeMagic

Reputation: 44571

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.

Upvotes: 4

Related Questions