Android Service系列(三)用service还是thread?

段落一 A service is simply a component that can run in the background, even when the user is not interacting with your application, so you should create a service only if that is what you need.

service的特点是能后台运行,即便用户不在和你的app进行交互,所以你要想好是否用service

段落二 If you must perform work outside of your main thread, but only while the user is interacting with your application, you should instead create a new thread. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), and stop it in onStop()

如果只是交互时候想在后台做别的事,用Thread就够了,没必要用service

段落三 Also consider using AsyncTask or HandlerThread instead of the traditional Thread class. See the Processes and Threading document for more information about threads.

AsyncTask 和 HandlerThread比单纯的Thread好用

段落四 Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

使用Service注意要自己开个线程

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值