开发者

How to cancel a thread

开发者 https://www.devze.com 2023-02-18 07:48 出处:网络
I am trying to make a media player which has a seekbar. As the seekbar should stop progress when pause is pressed or stop is pressed. I want to stop the thread accordingly. Moreover when back is press

I am trying to make a media player which has a seekbar. As the seekbar should stop progress when pause is pressed or stop is pressed. I want to stop the thread accordingly. Moreover when back is pressed I want to cancel the thread aga开发者_开发知识库in.


boolean running = true;
final Thread t = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            while(running){
                 //Your code
            }
        } catch (Exception e) {
        // TODO Auto-generated catch block
    e.printStackTrace();
    }
    //handler code

    }
});
t.start();

defining a "running" boolean will manage when the thread will execute its code, when you set running to false the seekbar will stop without interrupting or killing the thread.

0

精彩评论

暂无评论...
验证码 换一张
取 消