Run another application as soon as you receive an SMS

Asked

Viewed 1,808 times

6

I need to call an app already installed on my mobile as soon as I receive an SMS from a certain number.

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

Shall I wear Intent or something like that?

  • 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.

  • I recommend this link: http://blog.hachitecnologia.com.br/mobile/workingcom-broadcast-receivers-no-android

  • Just about Broadcast Receivers that I’m researching right now. Thank you

  • 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

1 answer

6


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’m having trouble opening an Activity from the receipt of the message, I saw in some places that it is an incorrect practice, but for this application I need a very quick response, so I need it to be done this way. What I need is to open a page right after receiving the message using the browser and for some detail I’m not getting.

  • Could you post more details? You’re doing startActivity with the newtask flag?

  • then, the code even shows the message as a notification, but beyond the message I need to open the page and to open the page I’m trying something like: //Intent Webintent = new Intent (Intent.ACTION_VIEW, Uri.parse ("http://192.168.0.157")); //startActivity (Webintent); I will edit the code part

  • Probably the problem you are having is that you are trying to start an Activity from a broadcast receiver, then you will need to put this flag: Webintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  • Eae warns me if it worked?

  • 1

    It did work. I’ll try to put the code here. My problem was this... I had a GPRS mobile connected to a Rduíno, so when pressing the bell was sent a text message to my cell, when receiving the text message the phone should open automatically with the link from my camera ip. And it worked

  • That’s cool! Put yes seems to be interesting! Abs

  • @user8728 If the answer was sufficient for your problem could accept the question?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.