tags:

views:

6222

answers:

1

I want to retrieve the SMS messages from the inbox and display them?

+6  A: 

It is a trivial process. You can see a good example in the source code SMSPopup

Examine the following methods:

public static SmsMmsMessage getSmsDetails(Context context,
                        long ignoreThreadId, boolean unreadOnly)
public static long findMessageId(Context context, long threadId, long _timestamp, int messageType
public static void setMessageRead(Context context, long messageId, int messageType)
public static void deleteMessage(Context context, long messageId, long threadId, int messageType)

this is the method for reading:

        public static SmsMmsMessage getSmsDetails(Context context,
                        long ignoreThreadId, boolean unreadOnly) {

                String SMS_READ_COLUMN = "read";
                String WHERE_CONDITION = unreadOnly ? SMS_READ_COLUMN + " = 0" : null;
                String SORT_ORDER = "date DESC";
                int count = 0;

                //Log.v(WHERE_CONDITION);

                if (ignoreThreadId > 0) {
//                      Log.v("Ignoring sms threadId = " + ignoreThreadId);
                        WHERE_CONDITION += " AND thread_id != " + ignoreThreadId;
                }

                Cursor cursor = context.getContentResolver().query(
                                SMS_INBOX_CONTENT_URI,
                      new String[] { "_id", "thread_id", "address", "person", "date", "body" },
                                WHERE_CONDITION,
                                null,
                                SORT_ORDER);

                if (cursor != null) {
                        try {
                                count = cursor.getCount();
                                if (count > 0) {
                                        cursor.moveToFirst();

//                                      String[] columns = cursor.getColumnNames();
//                                      for (int i=0; i<columns.length; i++) {
//                                              Log.v("columns " + i + ": " + columns[i] + ": "
//                                                              + cursor.getString(i));
//                                      }

                                        long messageId = cursor.getLong(0);
                                        long threadId = cursor.getLong(1);
                                        String address = cursor.getString(2);
                                        long contactId = cursor.getLong(3);
                                        String contactId_string = String.valueOf(contactId);
                                        long timestamp = cursor.getLong(4);

                                        String body = cursor.getString(5);

                                        if (!unreadOnly) {
                                                count = 0;
                                        }

                                        SmsMmsMessage smsMessage = new SmsMmsMessage(
                                                        context, address, contactId_string, body, timestamp,
                                                        threadId, count, messageId, SmsMmsMessage.MESSAGE_TYPE_SMS);

                                        return smsMessage;

                                }
                        } finally {
                                cursor.close();
                        }
                }               
                return null;
        }
Omer