如何在 Android 中创建聊天屏幕?
我正在创建一个 Android 聊天应用程序。我希望聊天屏幕包含一些功能,例如用于与不同用户聊天的新 UI 屏幕。但我不知道如何进一步推进。我应该使用服务还是异步任务或者还有其他方式吗?请帮助我开始。
I'm creating an Android chat application. I want chat screen to include features like a new UI screen for chat thread with a different user. But I don't know to move further with it. Should I use service or asyntask or is there any other way. Please help me get started.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不。如果您为每个用户使用每个线程,您将产生许多线程(内存、创建和清除的时间......)。
当接收消息时,您应该使用一个服务来侦听(通过使用意图,这是一项简单的工作),并且您可以在主 UI 线程中执行此操作。
No. if you use each thread for each user, you will spawn many threads (memory, and time to create and clear...).
You should use a service to listen when receive message (this is an easy work by using intent), and you can do in your main UI Thread.
一旦活动关闭,聊天就会关闭(与服务器/用户正在聊天的另一个设备的网络连接),所以我不认为有理由使用服务/AsyncTask。只需有一个线程等待来自套接字流的数据,并在数据到达后向用户显示该数据。
The chat gets closed (The network connection with the server/another device which the user was chatting with) once the activity closes, so I don't see a reason to use a serivce/AsyncTask. Just have a thread waiting for data from the socket streams, and show that data to the user once it arrives.