问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/18262418/custom-timeout-for-a-method-android