I am implementing a chat app using asmack library. I want to start a always running service where all the connection management task is to be done and that should be keep on receiving the incoming packets and saving the messages in my sqlite database. And as my app is launched I want my app to be notified too for the incoming messages while the background service is inserting the messages into sqlite database. How to implements such structure with sticky service. Is partial wake lock also needed. Since, images are also to be downloaded using http partially sometimes on specific demand.
Asked
Active
Viewed 4,802 times
1 Answers
16
You can start the service after registration on the server.
onStartCommand of service login the user.
Implement service with PacketListener,ConnectionListener,InvitationListener
PacketListener for Receiving Groupcaht or One to One chat messages.
ConnectionListener to check if connection to the server is connected or disconnected.
InvitationListener For getting Group invitations.
After successful login add these listeners.
Now when the message is received save that message in local database and send a Broadcast for the activities who want the message to be received.
Check if the app is in the background if yes then send the notification for the new message
Mail me if any buddy face any issue cvofjaspreet@gmail.com

Community
- 1
- 1

Jaspreet Chhabra
- 1,431
- 15
- 23
-
1Thanks a lot. I also want the app to keep on receiving messages while being in background. But for this the user must be online But as the user leaves the chat window the user gets offline in my app. – Rahul Rastogi May 05 '14 at 10:42
-
1Then your connection must be breaking Implement ConnectionListener to check where connection is closing. 1 up please for the answer – Jaspreet Chhabra May 05 '14 at 13:52
-
Actually, I want to start a "started service(unbind)" whose task is to handle everything from receiving messages to sending messages. I want my client is to be just like a interface that will issue the commands to service And the service will do the necessary. – Rahul Rastogi May 06 '14 at 04:29
-
For that there are listeners in the started service But how to send commands from app to service to do other work like sending message. – Rahul Rastogi May 06 '14 at 04:38
-
you can see this example http://dharmendra4android.blogspot.in/2012/05/bind-service-using-ibinder-class.html Other way of doing is send a broadcast to service – Jaspreet Chhabra May 06 '14 at 05:04
-
I am loading all the necessary data like rosters, vcard info, chat messages upto a certain date from sqlite to Manager classes(which provide data to UI). And I am worried about heap size. Is that ok! – Rahul Rastogi May 06 '14 at 09:36
-
Yes load the data on UI using lazy loading i.e load more data on scroll. It will work smoothly – Jaspreet Chhabra May 06 '14 at 11:25
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52107/discussion-between-user2155922-and-jaspreet-singh) – Rahul Rastogi May 06 '14 at 12:44
-
-
@Jaspreet/Rahul - do you have any links / sample code for the above mentioned service – pankaj Nov 09 '15 at 14:34