Using a BroadcastReceiver
it is possible to intercept the messages that are arriving on the device.
First you will need the following permission in your AndroidManifest.xml
, in this case I put to increase the priority this will help if you have another application (third party) that captures SMS, so it will have privilege at the time of receiving:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<receiver android:name="com.example.smsinterceptor.MessageReceiver" android:exported="true">
<intent-filter android:priority="999" >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
BroadcastReceiver MessageReceiver.java
, here in the receiver I get the message and all the details through the extras. Just be careful that if your message is composed by more than one SMS you will have to deal with; in the code below I only handle one message. Also after detecting that is the message I want I use the abortBroadcast()
, this will prevent other applications from capturing that same message (as long as the priority is ok on manifest):
public class MessageReceiver extends BroadcastReceiver {
private String TAG = MessageReceiver.class.getSimpleName();
private String NUMBER_FILTER = "3784";
public void onReceive(Context context, Intent intent) {
Log.e(TAG, "Receiver activated");
Bundle extraContent = intent.getExtras();
Object[] messagePdus = (Object[]) extraContent.get("pdus");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) messagePdus[0]);
if (smsMessage.getOriginatingAddress().endsWith(NUMBER_FILTER)) {
Log.e(TAG, "Message intercepted: "+ smsMessage.getMessageBody());
abortBroadcast();
}
}
}
I believe it is through the use of Broadcast Receivers. Take a look at this I told you about. I think that’s because in my project when I wanted to perform an action when a download on Android completed I had to use it. If I’m not mistaken you register Broadcasts to perform certain actions according to system events.
– Lucas Santos
I recommend this link: http://blog.hachitecnologia.com.br/mobile/workingcom-broadcast-receivers-no-android
– Lucas Santos
Just about Broadcast Receivers that I’m researching right now. Thank you
– user8728
Here’s a good example of how to receive the notification: http://androidexample.com/Incomming_SMS_Broadcast_Receiver_-_Android_example/index.php? view=article_discription&aid=62&aaid=87
– carlosrafaelgn