【问题标题】:How to kill the thread when the new activity is started and how to override the previous thread新活动启动时如何杀死线程以及如何覆盖之前的线程
【发布时间】:2012-05-14 04:28:02
【问题描述】:

当当前活动空闲 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 次活动中,时间已经结束,它会回到主页

当我进入下一个活动时我需要代码,它应该杀死前一个线程并创建一个新线程

否则

覆盖之前的线程

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    您需要使用一些布尔值来停止线程,该值在 Activity 的 onDestroy 或 onPause 上设置为 false。

    而(线程需要){ } 公共 onDestroy(){ 线程需要 = 假; }

    【讨论】:

    • 感谢您的回复,但是线程正在前台运行而不是后台屏幕是空白的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多