当现有活动空闲10秒时,我有10个活动,然后它应该返回主页
我在每项活动中使用的代码是
final Handler handler=new Handler();
final Runnable r = new Runnable()
{
public void run()
{
handler.postDelayed(this, 10000);
Intent intent = new Intent(Business_Personal.this,PrintoActivity.class);
startActivity(intent);
Thread.interrupted();
}
};
handler.postDelayed(r, 10000);
Thread thread = new Thread()
{
@Override
public void run() {
try {
while(true)
{
sleep(1000);
handler.post(r);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
如果我使用上面的代码然后直到第3次活动它的工作正常但在第3次活动时间已经结束并且它将回到主页
我需要代码,当我进入下一个活动时,它应该杀死前一个线程并创建一个新线程
或者
覆盖前一个帖子
答案 0 :(得分:0)
您需要使用一些布尔值来停止线程,该值在Activity的onDestroy或onPause上设置为false。
while(threadNeeded){ } public onDestroy(){ threadNeeded = false; }