Wednesday, 25 April 2012

Retrieve SMS

Today, let's see a quick example which demonstrates how to access to SMS stored on a phone :
                //Retrieves the URI, you can select a different content address depending on which SMS you want
                //1. Inbox = "content://sms/inbox"
                //2. Failed = "content://sms/failed" 
                //3. Queued = "content://sms/queued" 
                //4. Sent = "content://sms/sent" 
                //5. Draft = "content://sms/draft"
                //6. Outbox = "content://sms/outbox"
                //7. Undelivered = "content://sms/undelivered"
                //8. All = "content://sms/all"
                //9. Conversations = "content://sms/conversations"
                final Uri SMS_INBOX = Uri.parse("content://sms/inbox");

                //Retrieves all SMS (if you want only unread SMS, put "read = 0" for the 3rd parameter)
                Cursor c = context.getContentResolver().query(SMS_INBOX, null, null, null, null);
                //Get all lines  
                while (cursor.moveToNext()) {
                                //Gets the SMS information
                                String address = cursor.getString(cursor.getColumnIndex("address"));
                                String person = cursor.getString(cursor.getColumnIndex("person")); 
                                String date = cursor.getString(cursor.getColumnIndex("date")); 
                                String protocol = cursor.getString(cursorgetColumnIndex("protocol")); 
                                String read = cursor.getString(cursorgetColumnIndex("read")); 
                                String status = cursor.getString(cursorgetColumnIndex("status")); 
                                String type = cursor.getString(cursorgetColumnIndex("type")); 
                                String subject = cursor.getString(cursorgetColumnIndex("subject")); 
                                String body = cursor.getString(cursorgetColumnIndex("body"));
                                //Do what you want
And of course, don't forget to add the following permission to your AndroidManifest.xml.

1 comment:

  1. After a very successful first four seasons, the makers of Vikings are finally coming up with the fifth season. It will have some starcast changes and hopefully it is going to be even more exciting. You can check out my site for more details about the same and stay updated.
    Vikings season 5 cast