Custom timeout for a method - Android

余生颓废 提交于 2021-01-28 02:56:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!