Kaloer.com

RSS Feed

Incoming SMS Messages

June 24, 2009 by Mads Kalør

Filed under: Android, Programming

During the development of version 1.3 of Kaloer Clock, I wanted to show an icon when a new sms message was received. This was not the biggest problem, though. If the sms message was read, the icon should be invisible. Therefore, it was necessary to check the sms messages in the inbox using a URI.

The following guide shows how I did this.

To listen for incoming sms messages, you should use a BroadcastReceiver. The receiver will be called every time a sms is received. The code can look like this:

// SMS RECEIVER
final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSbr = new BroadcastReceiver() {

	@Override
	public void onReceive(Context context, Intent intent) {
        // Called every time a new sms is received
		Bundle bundle = intent.getExtras();
		if (bundle != null) {
		//This will put every new message into a array of SmsMessages. The message is received as a pdu,
		//and needs to be converted to a SmsMessage, if you want to get information about the message.
			Object[] pdus = (Object[]) bundle.get("pdus");
			final SmsMessage[] messages = new SmsMessage[pdus.length];
			for (int i = 0; i < pdus.length; i++)
				messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
			if (messages.length > -1) {
				//Shows a Toast with the phone number of the sender, and the message.
				String smsToast = "New SMS received from " + messages[0].getOriginatingAddress() + "\n'" + messages[0].getMessageBody()	+ "'";
				Toast.makeText(context, smsToast,Toast.LENGTH_LONG).show();
			}
		}
	}
};
//The BroadcastReceiver needs to be registered before use.
IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED);
this.registerReceiver(SMSbr, SMSfilter);

If you for example has an icon that shows that a sms message is unread, you will need to make the icon invisible agian when the user has deleted the message from his inbox. This can be done by a method that will check whether there is an unread sms message in the inbox, or not. The method will return true if there is an unread message, or false if not.

private boolean checkSMS() {
	//Sets the sms inbox's URI
	Uri uriSMS = Uri.parse("content://sms");
	Cursor c = getBaseContext().getContentResolver().query(uriSMS, null, "read = 0", null, null);
	//Checks the number of unread messages in the inbox
	if (c.getCount() == 0) {
		return false;
	} else
		return true;
}

However, this method will not be called by itself. It needs to be called sometimes (for example in the onResume() method) to check for an unread messages.

At last, remember to add these two uses-permission tags to your AndroidManifest.xml file:

<uses-permission id="android.permission.RECEIVE_SMS" />
<uses-permission id="android.permission.READ_SMS" />

3 response to “Incoming SMS Messages”

  1. Whinepedia.in is build on joomla 1.5 + Community builder and we store cell phone no of each user. User can manage their complaints thru web. It is now desired to receive complaints from user from their cell to the CMS and upload the SMS to respective user data. Can you throw some light on how this can be acheived?

  2. This article is about Android – not for Joomla!. But if you want the users to be able to send sms messages to your server, you need a SMS gateway. Unfortunately, i’m not experienced with SMS gateways, so I can’t tell you much about how your desire can be archeived.

  3. I would like know if you can make this anonymous class of BroadcastReceiver wake up on an incoming SMS? I have done this with a concrete class, but how would I set up an anonymous class in the manifest? What I want to achieve is to ‘embedd’ the Receiver in an activity to avoid the need for sending an extra wakeup intent from the Receiver to the Activity, which duplicates the Activity if it is already running. The FLAG_ACTIVITY_NEW_TASK seems to duplicate the Activity if it already running. In other words: How can I cover both cases with an active and a non-active Activity?

Leave a comment