Run another application as soon as it receives an SMS

6

I need to call an application already installed on my phone once I receive an SMS from a certain number.

All the code in this application is ready, but I do not know how to call it from that message.

Should I use Intent or something similar?

    
asked by anonymous 19.05.2014 / 18:05

1 answer

6

Using BroadcastReceiver , you can intercept incoming messages on the device.

First you will need the following permission on your AndroidManifest.xml , in this case I put it to increase the priority this will help if you have another application (third party) that captures SMS, so it will have privilege when receiving: / p>

<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 receiver I get the message and all the details through the extras. Just be careful that if your message is composed of more than one SMS you will have to deal; in the code below I only deal with a message. Also, after detecting the message I want I use abortBroadcast() , this will prevent other applications from capturing this same message (provided the priority is ok in the 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();
        }
    }
}
    
20.05.2014 / 17:34