I am developing a chat app. When a user connects to internet, the server pushes message notifications for him.
Should I use Service or IntentService for new message notifications? The service/intentservice must start on boot and never stop, even if we close the app.
I expect if you are using your own push notification system, it's probably based on keeping a connection/socket open with your backend. You'll have to do that on a background thread because Android doesn't allow network operations on the main thread.
IntentService
simply queues up start commands (issued via context.startService()
) and executes them serially on a background thread; once that queue is exhausted it stops itself automatically. This is useful for performing relatively short-lived tasks off the main thread and in a context with less erratic lifecycle than an Activity. That doesn't sound like the best fit for something that must be always alive, so I think you should stick to a regular Service
and manage your own worker thread to keep network operations in the background.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.