First of all, I want to apologize, since I'm pretty sure all the informations I need are already somewhere here on stackoverflow. Problem is, I found quite similar situations like mine, but not exactly this one, so I thought about asking about the correct modus-operandi to create a final "how-to" for this circumstance, to help future devs
I need to write a background service, UI-less, which will send and receive data from third part applications (which already exist, I just have to include inside the code to send/receive the data shared with my service)
Let's focus on the App-->send-->Service flow, since I guess the other way around is the same
From what I gathered here, I have to
- create a Service, which I'll start in my MainActivity right before finish() it
- declare an IntentFilter and addAction() my custom string to identify my action
- registerReceiver() my receiver and my filter
In the onReceive() of my BroadcastReceiver, I'll check if my intent.getAction() has the same string I was expecting (because Broadcasts are system-wide and is virtually send could intercept them, right?) - if so, I'll do my business
QUESTIONS:
- First of all, is this the correct modus operandi?
- My Service is running in the same thread as my Activity, right? So what happens if I finish() my MainActivity? What can I do to keep my service always running in background no matter what?
- In any app where I want to send data to be received from my service, do I just have to create an Intent("My_Action") (the same string I'm expecting on the other side) and do sendBroadcast(intent) ?
- Should I use Service or IntentService? What's better in my circumstance?
- Do I have to write anything in the Manifest, either in my Service or in the other 3rd-part apps?
Thanks a lot for your expertise, since this service will be a crucial part on a lot of machines I wanna be sure I'm doing it the best way, and not just some unstable make-it-work code