我正在使用handler.postDelayed方法为一些动画内容创建一些延迟。
这样我就可以用Mediaplayer播放一些歌曲了。用户可以通过单击next退出此操作类。但在下一个屏幕上,即使我在next按钮的onclicklistener中调用了stop方法,同样的歌曲仍在继续。
是否由于加载下一个活动后添加的时间延迟所致。有什么想法吗?
handler.postDelayed(new Runnable() {
public void run() {
mp = MediaPlayer.create(getApplicationContext(), R.raw.num2);
mp.start();
imageView1.setImageResource(R.drawable.countcat2);
}
}, 2000);发布于 2011-05-17 02:23:43
您是否添加了一个Log来查看是否调用了run()?我假设您的处理程序未注册;毕竟,postDelayed将一直等到循环程序再次启动。
我想你的动画比那些2000ms快吧?在你的活动消失后,你无论如何都不能调用你的处理程序,它正在访问imageView1,我认为它在onDestroy中被销毁了。
您可以考虑添加一个标志,强制在onDestroy中立即调用该操作,并且/或者您可以使用Timer。在计时器的情况下,确保在它被销毁后不要使用像imageView1这样的东西。
发布于 2013-04-08 09:06:40
使用线程。然后通过中断来停止它:
Public Thread getThread(final Handler handle, int delay)
{
return new Thread()
{
@Override
public void run()
{
try
{
synchronized(this)
{
Thread.sleep(delay);
handle.post(new Runnable()
{
@Override
public void run()
{
"the code to be exec."
}
});
}
}
catch(Exception e)
{
e.printStackTrace();
}
};
};
}或者,您可以使用postDelayed函数
Public Thread getThread(final Handler handle, int delay)
{
return new Thread()
{
@Override
public void run()
{
try
{
synchronized(this)
{
handle.postDelayed(new Runnable()
{
@Override
public void run()
{
"the code to be exec."
}
}, delay);
}
}
catch(Exception e)
{
e.printStackTrace();
}
};
};
}你会发现这两种方法有一点不同。要中断它,请执行以下操作:
Thread t = getThread(handle, delay);
t.start();
if(t != null)
{
if (t.isAlive())
{
t.interrupt();
}
}希望我能帮上忙。
https://stackoverflow.com/questions/6021610
复制相似问题