开发者

Stop thread at the time of closing application in android?

开发者 https://www.devze.com 2023-01-05 11:45 出处:网络
I am new to android.At the开发者_JAVA技巧 time of closing application i need to stop the thread in android.

I am new to android.At the开发者_JAVA技巧 time of closing application i need to stop the thread in android. Can anyone help me to solve this?


You could call the finish() Activity method as such:

@Override
    protected void onStop() {
        finish();
        super.onStop();
    }

So, this way when you hit the home button or back out of the application completely, it should end it.


The best solution is to not create the thread yourself in the first place. If the background thread is just to do a bit of work, consider using AsyncTask instead of your own thread. Or, if this is a service, consider using IntentService.

Otherwise, I hope your background thread is blocking on something (e.g., waiting on a socket, waiting on a LinkedBlockingQueue). In that case, you can terminate the thread by doing something to what it is blocking upon (e.g., closing the socket, sending a message on the LinkedBlockingQueue to tell the thread to fall out of its work loop).

0

精彩评论

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

关注公众号