0

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?

Bialy
  • 905
  • 2
  • 12
  • 22
  • How your foreground Service can "display items"? An Android Service doesn't has an Interface where display something, so you need to explain better WHO is displaying these items.... – emandt Mar 23 '22 at 10:56
  • A foreground service with a WindowManager – Bialy Mar 23 '22 at 11:00

1 Answers1

0

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
emandt
  • 2,547
  • 2
  • 16
  • 20
  • So in my case I presume they are on the same thread? As I'm not using any Handlers. But still how would be able to use onBind() and where the context should come from? The application? – Bialy Mar 23 '22 at 13:14
  • A normal Service runs in the Main/UIThread and an IntentService has its own thread. However best way is always using a Binder and onBind() method for the first type. – emandt Mar 23 '22 at 13:27