Issue
I am doing an app that will allow people to share files between them. Right now I am trying just to share text, but soon I will change it to Files. But I am having some trouble creating the Socket connection (the 2 devices are already connected via hotspot: one of them is the hotspot; the other one connects to it).
I was going to do a normal java socket connection, but then I read here that if the phone goes to sleep the sockets will close/timeout. The solution that you guys recomended for that problem was using Intent Services.
I created two IntentServices, a ClientCommService and a ServerCommService. I supose that inside the onHandleIntent() method, I should create the sockets and start listening/waiting for any incoming messages (create the stream objects and start the sockets). My problem is that i also want to send messages and I don't know where I should put the logic to send a message this.out.writeObject(). This is important, because i want to create a bidirectional communication, that can be started by any of the members (server or client).
Question 2: Some approaches that i saw here on stackoverflow, only created an IntentService for the Server. Why?
Solution
If you want to keep alive your service even after your phone goes sleep then you need to create Service
instead of IntentService
.
Also you need to keep your service as STICKY service.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
Remember that this service is running on main thread so you need to take care of your code wont interrupt UI thread.
You can put your client/server logic in onStartCommand
method.
Answered By - dharam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.