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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With