Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想精确地运行一些代码 20 秒。它类似于循环,但没有变量,我有时间(以秒为单位)。
我应该有这样的时间条件:
do { variable++ } while (sec < 20)
怎么可能在Android中做到这一点?
我的应用程序应该在用户按下按钮后运行这个 20 秒的代码。
您可以在可运行的 Android 中使用Handler类,然后使用 postDelayed() 方法。这样,您将能够在线程进度的 20 秒内更新 UI。一个很好的例子是hear。您的代码可能看起来像这样......
Handler handler = new Handler(); final Runnable r = new Runnable(){ public void run() { //Do thing after 20 sec } }; handler.postDelayed(r, 20000);