Incoming SMS Messages

Wed, 06/24/2009 - 16:09

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 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.

1
2
3
4
5
6
7
8
9
10
11
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:

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

Comments

mizkat
Mon, 08/02/2010 - 09:54

oh thats really great !!
thank you kaloer !!!
:D

Post new comment
The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.