Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sync between local service with a thread and an activity

I'm trying to think of a way on how to sync in between a local service and the main activity.

The local service has,

  • A thread with a socket connection that could receive data at any time.
  • A list/array with data.
  • At any time the socket could receive data and add it to the list.

The activity needs to display this data. So when the activity starts up it needs to attach or start the local service and fetch the list. It also needs to be notified if the list is updated.

I think I would need to sync my list somehow so the local service does not add a new entry to it while the activity fetches the list when connecting to the service.

Any ideas?

Thanks.

like image 921
Henrik Avatar asked Feb 26 '23 22:02

Henrik


1 Answers

I answered a somewhat similar question here. In this answer is a link to a presentation hold by mark brady on the droidconf in berlin. In his slides he describes a framework that manages this kind of things. He also offers the source for this on github.

He proposes the following solution. Build a controller object that lives in the scope of an custom application class. The controller starts the service or a simple worker thread and notifies the UI if it gets notified by the service that something changed. He advises against the use of AIDL if it is not absolutely necessary.

The android documentation also offers an example on how to start a local process.

like image 62
Janusz Avatar answered Mar 08 '23 07:03

Janusz