【问题标题】:Android: Restart the interrupted thread on onResumeAndroid:在 onResume 上重新启动中断的线程
【发布时间】:2016-05-14 10:50:27
【问题描述】:

我正在尝试重新启动 onResume 中的中断线程(我正在解释 onPause 中的线程)。为此,我在网上看到了很多示例,但没有任何帮助(可能是我的错)。所以,请告诉我如何在 onResume 中重新启动中断的线程

我的代码:

private void runThread(){  

 threadService = new Thread() {

    @Override
    public void run() {
        try {
            while (!isInterrupted()) {
                Thread.sleep(1000);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Log.e("Thread", "thread");

                        if (freshMSgId != null) {
                            getPrevChatVolleyInThread();
                        }
                    }
                });
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
 };

 threadService.start();

}

@Override
protected void onPause() {
super.onPause();

if (threadService != null) {
    threadService.interrupt();
}
}

【问题讨论】:

标签: android multithreading onresume onpause


【解决方案1】:

@npace 谢谢你,我从你的评论中得到了这个想法。我在 onResume 中重新启动了中断的线程,就像

@Override
protected void onResume() {
    super.onResume();
    runThread();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2016-04-22
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    相关资源
    最近更新 更多