Android,暂停和恢复处理程序回调
我有一个处理程序,如下使用:
handler.postDelayed(Play, 1000);
当我的应用程序 onPause() 在此完成之前被调用时,我需要暂停它并告诉它不要执行“postDelayed”,直到我恢复。
这可能吗,或者有其他方法吗?
我的问题是,当调用 onPause() 时,我会暂停音频(SoundManager),但如果此后调用此 handler.postDelayed,音频将不会暂停,并将继续在后台与我的应用程序一起播放。
@Override
public void onPause()
{
Soundmanager.autoPause()
}
但 1000 毫秒后 postDelayed 再次开始播放音频。
I have a handler that I am using as follows:
handler.postDelayed(Play, 1000);
when my application onPause() is called before this is done, I need to pause it and tell it not to perform the "postDelayed" until I resume.
is this possible, or is there an alternative way?
My problem is that when onPause() is called I pause the audio (SoundManager), but if this handler.postDelayed is called after that, the audio will not be paused and will continue to play with my application in the background.
@Override
public void onPause()
{
Soundmanager.autoPause()
}
but then the postDelayed after 1000ms starts the audio playing again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要继承
Handler
并实现暂停/恢复方法,如下所示(然后当您想要暂停消息处理时,只需调用handler.pause()
,并调用handler .resume()
当你想重新启动它时):You need to subclass
Handler
and implement pause/resume methods as follows (then just callhandler.pause()
when you want to pause message handling, and callhandler.resume()
when you want to restart it):你有没有尝试过:
Ger
Have you tried with:
Ger
修改CpcCrunch给出的答案。 handleMessage 对我不起作用,所以使用 dispatchMessage 代替它。注意:以下代码是用 Kotlin 编写的:
Modifying the answer given by CpcCrunch. There handleMessage not worked for me, so instead of it using dispatchMessage. Note: Below code is written in Kotlin:
当想要暂停/恢复队列中的
Runnables
时,我想出了一个 CpnCrunch 的替代方案。要拥有在仍然连接且离线时调用的方法,一旦在线,恢复队列并执行所有可运行对象。不要使用
Handler
,而是使用ExecutorService
:使用它与 Handler 类似,但不要使用
post(runnable)
,而是使用execute(可运行)
I came up with an alternative to CpnCrunch when wanting to pause/resume
Runnables
in a queue. To have methods that has been called whilst still connecting and is offline, once online, resume the queue and all runnables are executed.Instead of using
Handler
, useExecutorService
:Using it is similar to Handler, but instead of
post(runnable)
, useexecute(runnable)