f20k
f20k

Reputation: 3106

Android - Shared networking between activities

I am developing an application which utilizes a custom network protocol over TCP. Several tasks within the application use different activities however each activity requires the networking enabled (since each activity has to send/receive some custom packets over the network). So my idea is basically to

  1. At application start - create a service to handle networking ( I read somewhere that I should do the networking in another thread in this service to prevent ANR)

  2. When a new activity is run, it binds to that service. During that time it sends/receives data from the service. When the activity ends, it unbinds from the service.

I would like to know if this makes sense.

Also, I understand that I can send data to the service (to send over the network) using the aidl interface, but how would I receive data from the service? Polling through the aidl seems to be the only way I can think of - which means I would need another thread in each of my activities to prevent ANR. Surely there is some better way.

I appreciate your help and am open to suggestions.

Upvotes: 0

Views: 126

Answers (1)

f20k
f20k

Reputation: 3106

Actually I've been reading more and am beginning to look at callback methods in the sample code provided here. After reading it more thoroughly I have a better understanding of the services and AIDL.

Upvotes: 0

Related Questions