Incoming SMS Messages
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" /> |
Popular blog posts
-
Android Preferences
Preferences are an important part of an Android application. It is important to let the users have the choice to modify...
-
Incoming SMS Messages
During the development of version 1.3 of Kaloer Clock, I wanted to show an icon when a new sms message was received....
-
Permissions in Kaloer Clock
Android application security has recently become a focus of interest when talking about Android. A chinese developer...
-
Android Plurals
A useful, but sadly rarely used string resource is plurals. Plurals are used for words or phrases which are spelled...
Comments
oh thats really great !!
thank you kaloer !!!
:D
Exc.work.Ideal for my needs.
Keep up good work,will support
and talk it up.Keep on rollin'.
Hi,
I know only programming for Windows Mobile by C#. So I do not know how to develop applications for Android, and I have never seen appl like I have in my head on internet. Please, may I ask you to develop it?
The application is listening for incomming SMS. When SMS is reveived the appl saves the content of SMS to text file. Text file will have only one line - body of SMS. The file name is consisting of sender phone number and date&time when SMS has been received. The target location (directory/folder) where all these text files will be located can be set over simple config files. It can be possible to configure target directory to be located on external (e.g. microSD) card.
Please, can you develop such an easy application for Android? :-)
For Windows Mobile in C# it is one hour task :-)
Thanks,
Martin
Post new comment