They can be used very differently for different purposes.
AsyncTask
is a handy threading utility that can be used if you need to constantly tell the user something or periodically perform operations on the main thread. It offers a lot of fine-grain control, and because of it's nature is very easy to work with in the Activity
whereas an IntentService
generally requires using the BroadcastReceiver
or IBinder
framework.
IntentService
can be used very much like an AsyncTask
, but it's purpose is meant for background downloading, uploading, or other blocking operations that don't need user interaction or main thread. For example, if you want to download and cache maps, you may want to call an IntentService
so the user doesn't have to be looking at the app for it to download. Likewise, if you're sending data to your server, an IntentService
is extremely helpful in this regard because you can start and forget. The user can, say, type a comment in your app then press "send". "Send" will launch the IntentService
which gets the comment and send it off in to your server on a background thread. The user could press "send" and leave the app immediately and the comment will, eventually, still reach your servers (assuming no errors of course). If you did this with an AsyncTask
in your Activity
on the other hand, the system could kill off your process in the middle of your exchange and it may-or-may not go through.
Generally speaking, neither are meant for long running applications. They're meant for short, one-off operations. They could be used for permanent, or long-running actions but it's not recommended.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…