3

I want to create a service that will have no user interface and always run in the background - looking for an SMS with a particular substring, and then responding with an SMS of its own (sent programmatically, without user intervention). Does anybody know how to do this?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Possible duplicate as [this SO question](http://stackoverflow.com/questions/990217/android-app-with-service-only) – yorkw Nov 28 '11 at 02:53
  • @yorkw: Not necessarily. The requirement is for periodic processing of some event or other (in this case an incoming SMS). There's no need to run a service permanently as in the OP's question in that link. Using a `BroadcastReceiver` and `IntentService` would do the job. – Squonk Nov 28 '11 at 03:19

2 Answers2

7

To expand on Darth Beleg's answer. It's not actually possible to do what you're describing (at least not exactly).

The only way you can detect an incoming SMS, as Darth explains, is to listen for an Intent. The reason why it's not possible to do exactly what you want is that it's not possible for a Service to listen for an Intent.

I'd recommend a BroadcastReceiver which listens for the Intent and then starts an IntentService (rather than a Service) and respond accordingly. IntentServices don't run all of the time - they're started, do their work then shutdown. A much more efficient way of doing what you're looking for.

Squonk
  • 48,735
  • 19
  • 103
  • 135
5

You don't need an always-running service for this task - just set up a BroadcastReceiver listening for intents with action "android.provider.Telephony.SMS_RECEIVED". See this article for an example.

Darth Beleg
  • 2,657
  • 24
  • 26