开发者

Android: running a thread in background

开发者 https://www.devze.com 2022-12-20 14:46 出处:网络
is there any way to leave a thread in background when i close the app in android? I read about a Service but implementing it is too much than i need. 开发者_开发知识库Thanks

is there any way to leave a thread in background when i close the app in android? I read about a Service but implementing it is too much than i need. 开发者_开发知识库Thanks


Hi, is there any way to leave a thread in background when i close the app in android?

That will happen by default. That does not mean it is a good idea.

I read about a Service but implementing it is too much than i need.

More likely, it is precisely what you need.

If you start a thread in an Activity, then fail to stop that thread, it will run forever, until Android terminates the process, since you have no way of stopping it. The only sort-of acceptable exception is if your thread stops itself, and then you have to be very very careful that it actually does terminate cleanly. On the other hand, you can arrange to stop a running Service whenever you need, from any Activity, and that Service can arrange to stop the thread, so you don't leak the thread indefinitely.

I strongly encourage you to implement a service that manages the thread.


Another reason to use a Service is that the platform will give the process priority to NOT be killed while it is in the background.

0

精彩评论

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

关注公众号