[英]Android start/stop repeating Thread after specific time
I need a thread repeating after 30 seconds that load data from web service. 我需要一个30秒后重复加载来自Web服务数据的线程。 i want to stop and start thread onResume and onPause so that it runs when screen is active and disable it when activity paused.
我想停止并启动线程onResume和onPause,以便它在屏幕处于活动状态时运行,并在活动暂停时禁用它。 Restart on resume.
重启继续。
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
dataIfInternet(refreshTop);
}
}, 30000);
How to stop it when activity paused. 如何在活动暂停时停止它。
U can't pause the handler, so u need to cancel it in your onPause
and post in again in your onResume
你不能暂停处理程序,所以你需要在你的
onPause
取消它并在你的onResume
中再次发布
so u probably want something like this: 所以你可能想要这样的东西:
private Handler myHandler = new Handler();
in your onResume()
在你的
onResume()
Runnable myRunnable = new Runnable()
{
@Override
public void run()
{
dataIfInternet(refreshTop);
}
};
myHandler.postDelayed(myRunnable, 30000);
and in your onPause()
并在你的
onPause()
// Cancel the runnable
myHandler.removeCallbacks(myRunnable);
I hope this solves your problem 我希望这能解决你的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.