【发布时间】:2010-03-18 21:43:53
【问题描述】:
我有一个 ImageView,其中图片每 5 秒切换一次,我正在尝试添加暂停和
恢复按钮,可以停止和重新启动操作。我正在使用 Handler、Runnable 和
postDelay() 用于图像切换,我将代码放在 onResume 上。我正在考虑使用
等待并通知暂停和恢复,但这意味着创建一个额外的线程。所以
远远的线程,我有这个:
类RecipeDisplayThread 扩展线程 { boolean pleaseWait = false;
// This method is called when the thread runs
public void run() {
while (true) {
// Do work
// Check if should wait
synchronized (this) {
while (pleaseWait) {
try {
wait();
} catch (Exception e) {
}
}
}
// Do work
}
}
}
在主要活动的onCreate()中:
Button pauseButton = (Button) findViewById(R.id.pause);
pauseButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
while (true) {
synchronized (thread)
{
thread.pleaseWait = true;
}
}
}
});
Button resumeButton = (Button) findViewById(R.id.resume);
resumeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
while (true) {
// Resume the thread
synchronized (thread)
{
thread.pleaseWait = false;
thread.notify();
}
}
}
});
暂停按钮似乎可以工作,但之后我无法按下任何其他按钮,例如
恢复按钮。
谢谢。
【问题讨论】:
-
请选择比“暂停”和“继续”更有用的标签。
标签: android