-1

Is it possible to read incoming sms and reply back only to specific or desire number. i-e i want to make app that will run in back ground ... whenever i will send sms with my number it will automatically respond back to me with XYZ information

wtsang02
  • 18,603
  • 10
  • 49
  • 67

1 Answers1

4

Yes, it's possible. You need to add a BroadcastReceiver in your app that will intercept incoming SMS messages and then send a message if it matches the number you're looking for.

Following source code from:
BroadcastReceiver + SMS_RECEIVED
http://shreymalhotra.me/blog/tutorial/receive-sms-using-android-broadcastreceiver-inside-an-activity/

In AndroidManifest.xml:

<receiver android:name=".SMSReceiver"> 
  <intent-filter android:priority="999"> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
  </intent-filter> 
</receiver>

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

In a new file called SMSReceiver.java:

public class SMSReceiver extends BroadcastReceiver 
{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
        // get SMS data, if bundle is null then there is no data so return
        Bundle extras = intent.getExtras();
        if (extras == null) return;

        // extract SMS data from bundle
        Object[] pdus = (Object[]) extras.get("pdus");
        for (int i = 0; i &lt; pdus.length; i++) {
            SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
            String sender = SMessage.getOriginatingAddress();
            String body = SMessage.getMessageBody().toString();

            // if there's an SMS from this number then send
            if(sender.equals("+1800555555") {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(sender, null, "sms received", null, null);
            }
        }
    }
}
Community
  • 1
  • 1
Eric Ahn
  • 716
  • 1
  • 5
  • 15