Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:Receiving SMS from a specific number?

Tags:

android

sms

I am new to android and I am working on an application in which I can set the phone number from whom I receive SMS and only that SMS will be acknowledged by the application. Can any one help me please?

like image 443
aa051 Avatar asked Feb 20 '26 06:02

aa051


1 Answers

Here is a code snippet -

public class SMSApp extends IntentReceiver {

static final String ACTION =
        "android.provider.Telephony.SMS_RECEIVED";

public void onReceiveIntent(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION)) {
        StringBuilder buf = new StringBuilder();
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
            for (int i = 0; i < messages.length; i++) {
                SmsMessage message = messages[i];
                String phNum = message.getDisplayOriginatingAddress();
                if ("xxx-xxx-xxxx".equals(phNum))
                {// Do your thing }
            }
        }

    }
}

You will of course need to put a receiver and a permission in the manifest similar to -

<uses-permission id="android.permission.RECEIVE_SMS" />
<application>
    <receiver class="SMSApp">
        <intent-filter>
            <action android:value="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
</application>
like image 196
Suchi Avatar answered Feb 23 '26 23:02

Suchi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!