简体   繁体   English

适用于whatsapp等应用的Android服务或IntentService?

[英]Android Service or IntentService for app like whatsapp?

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? 我应该使用Service或IntentService进行新的消息通知吗? The service/intentservice must start on boot and never stop, even if we close the app. 即使我们关闭应用程序,服务/ intentservice也必须在启动时启动并永不停止。

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. 您必须在后台线程上执行此操作,因为Android不允许主线程上的网络操作。

IntentService simply queues up start commands (issued via context.startService() ) and executes them serially on a background thread; IntentService简单地将启动命令排队(通过context.startService()发出)并在后台线程上串行执行; 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. 这听起来不太适合必须永远存在的东西,所以我认为你应该坚持使用常规Service并管理自己的工作线程以保持网络操作在后台运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM