1

Possible Duplicate: BroadcastReceiver + SMS_RECEIVED

Lets say i have an application i want to launch every time my phone receives a specific text message, a keyword for example. Can i do this if my application is not running? What' s a good way to do it?

I have never tried this before and i want to run an application on one phone which will send a specific text message to another phone (done so far), then the 2nd phone would start an application when the message is received (after checking the message to see if its the keyword).

Community
  • 1
  • 1
kotsosh
  • 187
  • 1
  • 2
  • 8
  • googling a little would have provided answers, like http://stackoverflow.com/questions/1973071/broadcastreceiver-sms-received – rds Dec 08 '11 at 16:18
  • yeah my problem is that i dont want my application to be running when the sms is received. It should start afterwards. Maybe i am missing something? – kotsosh Dec 08 '11 at 16:25
  • yes, you did not understand that a statically decalred boradcastreceiver will start the application if it is not currently running – rds Dec 08 '11 at 17:05
  • yeah i tried out a couple of examples i found and i think i got it now tyvm :) – kotsosh Dec 08 '11 at 17:51

2 Answers2

1

You need to write a BroadcastReceiver with the following intent filter

  <intent-filter> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
  </intent-filter> 
rds
  • 26,253
  • 19
  • 107
  • 134
  • in the manifest, the `` tag declares a `BroadcastReceiver` class that is available as part of the package's application components, __allowing the application to receive actions or data broadcast by other applications even if it is not currently running.__ – rds Dec 08 '11 at 17:03
  • One last thing, i have made my Receiver class which extends to BroadcastReceiver, it successfully receives the sms and reads its message. How am i supposed to run my main class of my application from there? again ty for your help. – kotsosh Dec 08 '11 at 18:18
  • There is no such thing as main class in Android framework. From the [application fundametals](http://developer.android.com/guide/topics/fundamentals.html#Components), you can start an Activity or a Service. – rds Dec 08 '11 at 18:21
  • And please close this thread by accepting the answer; and ask a new question if needed (even though, in that case, reading the "getting started" tutorials would probably help) – rds Dec 08 '11 at 18:23
0

You'll need to create an AlarmManager for this with a BroadcastReceiver in order to receive data when your application is not running, I believe.

Although this may not be possible, as it could be considered a security risk to read the contents of a user's text messages... this would require some nasty permissions, and your users may not want to use the software because of that. Try taking a look at some of the Amazon free app of the day reviews to see how bad this can be for your app.

Other than that, you should be able to use the classes stated above in order to implement this. Let me know if this isn't clear and I'll try to expand upon it.

EDIT:

My mistake, a more appropriate way to handle this would be through a Handler with a background Service. I haven't personally used this myself, so I can't tell you much more than the documentation. Try reading the documentation and looking at the examples :)

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • Ok then, maybe i could just make it a bit general just based on the telephone number. I would add the broadcast receiver on the manifest and then im kinda buffled. How would i start my app if its not running to begin with? Do you have any reference link were i could maybe read a bit more on the matter? Cause i feel i miss something here – kotsosh Dec 08 '11 at 15:59