0

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:

  1. First of all, is this the correct modus operandi?
  2. 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?
  3. 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) ?
  4. Should I use Service or IntentService? What's better in my circumstance?
  5. 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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shark107
  • 27
  • 4

1 Answers1

1
  1. Depending on how you want the "send data" process to be started, there are different approaches. It appears the send is short lived - so an IntentService is appropriate. If always from some user action in your activity, just call startService. If exposing send so other apps (not yours) can use it see Create an android service which can be called only by certain apps . There are use cases where starting the service is via a broadcast - see https://blog.nraboy.com/2014/10/use-broadcast-receiver-background-services-android/.

  2. Yes, services run on app (not tied to any activity) thread. Finishing an activity has nothing to do with a running (started) service - the service will continue to run. If you want the service to run (no matter what), you have to obtain a wake lock inside your service.

  3. If you want to send data (via service) from inside your app, just call startService(). I'm not sure if you are asking for other apps to be able to send data via your service.

  4. See Service vs IntentService or What is the difference between an IntentService and a Service?

  5. A service must be defined in your manifest. See http://developer.android.com/guide/topics/manifest/service-element.html and http://developer.android.com/guide/components/services.html Maybe you are asking more about defining intents to start the service (Purpose of Service Intent-Filter inside Manifest.xml)?

Community
  • 1
  • 1
Dave Truby
  • 193
  • 1
  • 5
  • Thanks for all the useful links. Yes, I was asking for other apps to be able to send data via my service, so I tried doing it the way I explained in OP and it works, so I guess I'll stick with it. I mostly wanted to know if this procedure was viable, aside from just " it works ", but since no one stoned me to death after my explanation I guess it's ok like that :D – Shark107 Apr 14 '15 at 15:00