Tiago Veloso Tiago Veloso - 3 months ago 18
Android Question

Android SMS Listener for specific number

I am writing an application that communicates with other devices through SMSs.

I was able to implement a listener for received SMSs. The thing is that it is listening to all received messages. I would like to only listen received SMSs from a specific number.

Here is my

onReceive
method.

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Bundle bundle = intent.getExtras();

Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}

// show first message
Toast toast = Toast.makeText(context,
"Received SMS: " + smsMessage[0].getMessageBody(),
Toast.LENGTH_LONG);
toast.show();
}

Answer

There is no way AFAIK to filter for a number just by the intent.

What you CAN do is to get phone number in the Receiver like this:

SMSMesssage msg=SMSMessage.creatFromPdu(pdu)
String origination_no = msg.getDisplayOriginatingAddress()