Sunday, May 31, 2015

Android Listening for SMS intents

Basically, need to create a Broadcast receiver. Some of the devices, we may need to also give the priority so that the message is received. 

public class SmsListener extends BroadcastReceiver{
    
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
            for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
                String messageBody = smsMessage.getMessageBody();
            }
        }
    }
}

Below need to be added in the manifest file 

".listener.SmsListener">
"2147483647">
"android.provider.Telephony.SMS_RECEIVED" />

Also, below permission should be given in the manifest file. 

"android.permission.RECEIVE_SMS" />

References:

No comments:

Post a Comment