0

We need to read both the inbound and outbound text messages from a phone. I have done much googling and have found a number of sources, such as:

Detecting SMS incoming and outgoing How to use SMS content provider? Where are the docs? Android read SMS messages

to name a few.

From my research, I have written the following code:

    Uri allMessages = Uri.parse("content://sms/inbox");
    Cursor cursor = airApp.getContentResolver().query(allMessages, null, null, null, null);

    if(cursor.moveToFirst()){
        do {
            for (int i = 0; i < cursor.getColumnCount(); i++) {
                log.debug(cursor.getColumnName(i) + "=" + cursor.getString(i) + "");
            }
            log.debug("One row finished **************************************************");
        } while (cursor.moveToNext());
    }

The problem is that cursor is always null. I checked the permissions and they are:

    <uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.RECEIVE_MMS" />

Any ideas why I'd be getting a null cursor?

Community
  • 1
  • 1
Thom
  • 14,013
  • 25
  • 105
  • 185
  • I think the problem is with your Uri, you have to add Uri.parse("content://sms/inbox") instead Uri.parse("content://sms/") to read inbox , I mean you are not specified the category (inbox / outbox / sent) to read. – theapache64 Feb 25 '15 at 15:28

1 Answers1

0

This is how i collect user messages

Context mContext = c;

// The category can be only "inbox" or "sent" or"outbox"
String category = "inbox";

        Cursor c = tempContext.getContentResolver().query(Uri.parse("content://sms/" + category), null, null, null, null);

        // Message Storages
        StringBuffer messages = new StringBuffer("");


        if (c.moveToFirst()) {

            for (int i = 0; i < c.getCount(); i++) {
                messages.append("################\nFrom:"
                        + c.getString(c.getColumnIndex("address"))
                        + "\nMessage:    "
                        + c.getString(c.getColumnIndex("body"))
                        + "\n#################\n\n");
                c.moveToNext();
            }

          Log.d("X","INBOX:"+messages);

        }

After working this snippet the StringBuffer called 'messages' will contain all the messages from inbox.

NOTE: The category can be only "inbox" or "sent" or"outbox" NOTE: You have to only add
in the manifest

theapache64
  • 10,926
  • 9
  • 65
  • 108