方法的自定义超时 - Android [英] Custom timeout for a method - Android
问题描述
在我的 android 应用程序中,我使用一种方法尝试在自定义 java 类中获取用户的当前位置.我想设置一个超时,当它到期时,可以做任何我想做的事情.我也希望可以取消超时.
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?
谢谢
推荐答案
您可以通过多种方式做到这一点
You can do this in many ways
Thread.sleep() (请在后台线程中)
您有很多选择,这取决于您想如何实施它以及您需要什么.The Google 和 SO 上都有许多示例和教程.
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
听起来不错,因为您可以在 millis
中设置时间并在 onFinish()
中执行您想要的操作,但任何都可以工作.
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.
这篇关于方法的自定义超时 - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!