Home > front end >  Comunication between a running android service and a recyclerview adapter inside the same service
Comunication between a running android service and a recyclerview adapter inside the same service

Time:03-24

So I have a foreground service displaying a few items, I want to perform some function in the enclosing service when I press on an item in the recycler view. I'm aware I can use an Instance of the service, but it's a memory leak. I'm aware as well of the binding/unbinding methods between service and activity, but this I believe doesn't apply to my situation?

CodePudding user response:

Normally everything you would like to execute in the Service should be implemented and then called using a Binder (if Service and App are in the same Process) and its onBind() method.

In your specific case I suppose your Service is the "owner" of the UI created by WindowManager (like a floating window), so:

  • if Service and UI are in the same Thread, you can pass a Listener/Callback to it or use the common onBind() way
  • if Service and UI are in different Theads: you need some kind of synchronization between them (Looper Message, a Queue, etc...)
  • if Service and UI are in different Process: you need to implement "AIDL" technique
  • Related