如何在Android中暂停/睡眠线程或进程?
我想在两行代码之间暂停一下,让我解释一下:
->用户单击一个按钮(实际上是一张卡片),我通过更改该按钮的背景来显示它:
thisbutton.setBackgroundResource(R.drawable.icon);
->假设 1 秒后,我需要通过更改背景来返回到按钮之前的状态:
thisbutton.setBackgroundResource(R.drawable.defaultcard);
->我尝试使用以下命令暂停这两行代码之间的线程:
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但是,这不起作用。也许我需要暂停的是进程而不是线程?
我也尝试过(但它不起作用):
new Reminder(5);
这样:
public class Reminder {
Timer timer;
public Reminder(int seconds) {
timer = new Timer();
timer.schedule(new RemindTask(), seconds*1000);
}
class RemindTask extends TimerTask {
public void run() {
System.out.format("Time's up!%n");
timer.cancel(); //Terminate the timer thread
}
}
}
如何暂停/睡眠线程或进程?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
解决此问题的一种方法是使用 Handler.postDelayed() 方法。一些 Google培训材料建议了相同的解决方案。
然而,有些人指出,上述解决方案 会导致内存泄漏泄漏,因为它使用非静态内部匿名类,该类隐式保存对其外部类(即活动)的引用。当活动上下文被垃圾收集时,这是一个问题。
避免内存泄漏的更复杂的解决方案是使用活动内的静态内部类对
Handler
和Runnable
进行子类化,因为静态内部类不持有对其外部类的隐式引用:请注意,
Runnable
使用 WeakReference 到 Activity,这在需要访问 UI 的静态类中是必需的。One solution to this problem is to use the Handler.postDelayed() method. Some Google training materials suggest the same solution.
However, some have pointed out that the solution above causes a memory leak because it uses a non-static inner and anonymous class which implicitly holds a reference to its outer class, the activity. This is a problem when the activity context is garbage collected.
A more complex solution that avoids the memory leak subclasses the
Handler
andRunnable
with static inner classes inside the activity since static inner classes do not hold an implicit reference to their outer class:Note that the
Runnable
uses a WeakReference to the Activity, which is necessary in a static class that needs access to the UI.您可以尝试这个,它简短
警告:永远不要在 UI 线程上执行此操作。
用它来睡觉,例如。后台线程。
您的问题的完整解决方案将是:
这是可用的 API 1,
无需创建 tmp Handler。此外,这个解决方案比 @tronman 更好,因为我们不通过 Handler 保留视图。
此外,我们在错误线程中创建的处理程序也没有问题;)
文档
ms,以毫秒为单位的正常运行时间。 View 类中的 postDelayed 代码:
You can try this one it is short
WARNING: Never, ever, do this on a UI thread.
Use this to sleep eg. background thread.
Full solution for your problem will be:
This is available API 1
Without creating tmp Handler. Also this solution is better than @tronman because we do not retain view by Handler.
Also we don't have problem with Handler created at bad thread ;)
Documentation
Code for postDelayed from View class:
我用这个:
I use this:
我使用倒计时
I use CountDownTime
您可能不想那样做。通过在按钮单击事件处理程序中放置显式
sleep()
,您实际上会锁定整个 UI 一秒钟。一种替代方法是使用某种单次计时器。创建一个 TimerTask 将背景颜色更改回默认颜色,然后在计时器上安排它。另一种可能性是使用 Handler。有教程,介绍了不再使用计时器的人使用处理程序。
顺便说一句,您无法暂停进程。一个Java(或Android)进程至少有1个线程,并且只能休眠线程。
You probably don't want to do it that way. By putting an explicit
sleep()
in your button-clicked event handler, you would actually lock up the whole UI for a second. One alternative is to use some sort of single-shot Timer. Create a TimerTask to change the background color back to the default color, and schedule it on the Timer.Another possibility is to use a Handler. There's a tutorial about somebody who switched from using a Timer to using a Handler.
Incidentally, you can't pause a process. A Java (or Android) process has at least 1 thread, and you can only sleep threads.
这就是我在一天结束时所做的 - 现在工作正常:
This is what I did at the end of the day - works fine now :
除了 Yankowsky 先生的答案之外,您还可以使用
postDelayed()
。这在任何View
(例如您的卡)上都可用,并且需要一个Runnable
和一个延迟时间。它会在延迟后执行 Runnable。In addition to Mr. Yankowsky's answers, you could also use
postDelayed()
. This is available on anyView
(e.g., your card) and takes aRunnable
and a delay period. It executes theRunnable
after that delay.这是我的示例
创建一个 Java Utils
This is my example
Create a Java Utils
或者您可以使用:
其优点是不需要包装
try ... catch
。Or you could use:
which has the advantage of not requiring a wrapping
try ... catch
.如果您使用 Kotlin 和 协程,您可以简单地执行
以下操作如果您需要更新 UI
If you use Kotlin and coroutines, you can simply do
And if you need to update UI
我知道这是一个旧线程,但在 Android 文档中我找到了一个非常适合我的解决方案...
https://developer.android.com/reference/android/os/CountDownTimer.html
希望这对某人有帮助......
I know this is an old thread, but in the Android documentation I found a solution that worked very well for me...
https://developer.android.com/reference/android/os/CountDownTimer.html
Hope this helps someone...
并确保您可以将其与“静态类”方法结合起来,如 tronman 答案中所述
And to be double sure you can be combined it with the "static class" method as described in the tronman answer