Daniel Wilson
Daniel Wilson

Reputation: 19824

Long running client-server connection between two devices

If two (or more) devices are connected to the same network, and each has my apk installed, how might one device efficiently 'talk' to the other? Google Play services, Wifi Direct, and bluetooth is unfortunately not available on these devices.

I thought of using a 3rd party push notification service, but ideally I need the response between either device to be as fast as possible, and long-lived.


I have managed to get two devices sending messages to one another using the old client-server Network Discovery Sample app in the docs. However, if either of the apps is closed or leaves memory, the connection is obviously broken. Therefore I'm trying to figure out if this is possible through a Service, which I understand exists outside of the Activity lifecycle.

I understand how an Activity might connect to a Service to send a message (good sample on that located here), but from what I gather this all happens locally on the device. Is it possible to have this exchange happen over a local network, from one app to another? I guess what I'm saying is how can I set up a basic client server socket relationship between two apps that won't die?

Upvotes: 2

Views: 151

Answers (1)

user3802077
user3802077

Reputation: 849

It has been a long time but it should still work. The problem here, as I understand it, is to have something that keeps running when the app is gone.

I remember using IntentService for this purpose. In the onHandleIntent() we made it loop while(!stopCondition) {...}

It was a stable solution then but it was around kitkat's time.

I'd try with the solution in your first paragraph being executed and managed by the IntentService which should keep it available.

Upvotes: 2

Related Questions