0

I am trying to read sms in android. In my manifest file, I have:

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

My Code:

Cursor cursor = getContentResolver().query(Uri.parse("content://mms-sms/inbox"), null, null, null,null);
((TextView) findViewById(R.id.info1)).setText("Number of messages: " + cursor.getColumnCount());

The problem is that I have more than 17 smses in my inbox but the output it gives is

Number of messages: 17

I have looked at this, this, this and this. Can someone please tell what the problem is?

Community
  • 1
  • 1
harshit
  • 3,788
  • 3
  • 31
  • 54

1 Answers1

3

You are asking for the column count:

cursor.getColumnCount()

Which is very different from what you want:

cursor.getCount()

getCount() returns the number of rows in a Cursor, or in this case the number of messages in your inbox.

getColumnCount() returns the number of columns in each row; like id, sender, message, time, etc. Read the second answer here: How many database columns associated with a SMS in android?

Community
  • 1
  • 1
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Ohhh got it, columnCount tells me the attrubutes, Count tells me the entries. Isnt it??? Accepting answer after three minutes. sof says that – harshit Oct 05 '12 at 18:08