段落一 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注意要自己开个线程