2

I tried to get sms list and below code works well in some devices but not working in other devices. Details tested with four devices using below code.

LG optimus one[android 2.2] - works well. SS galaxy s3[android4.0.4] - works well. SS galaxy s2[android 2.3.5] - not working. SS galaxy s2[android 4.0.4] - not working.

It seems that result depends on devices not android version because two devices with same android version[4.0.4] show differently. Symptom in the devices not working is that c.getCount() = 0 even if they have many sms. The query returns nothing. Why these are? How can I get sms list in galaxy s2 as well?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

String smsMsgBody = null;
String smsMsgAddress = null;
String smsMsgDirection = null;      

Uri uri = Uri.parse("content://sms");
// Uri uri = Uri.parse("content://sms/inbox");
// Uri uri = Uri.parse("content://sms/conversations/");

Cursor c= getContentResolver().query(uri, null, null,null,null);

// startManagingCursor(c);
if (c.getCount() > 0) 
{
String count = Integer.toString(c.getCount());
while (c.moveToNext())
{
smsMsgBody = c.getString(c.getColumnIndex("body"));
smsMsgAddress = c.getString(c.getColumnIndex("address"));
smsMsgDirection = c.getString(c.getColumnIndex("type"));

// Do things using above sms data
}
}
c.close();
}
  • 1
    "Why these are?" -- until Android 4.4, the SMS `ContentProvider` was undocumented and unsupported. There is no requirement for any SMS app to store messages in this provider. – CommonsWare Feb 08 '14 at 21:58
  • My test shows that sms query works in Android 4.0 & 2.3 but not in 2.3 This is what I can't understand. – user3288131 Feb 08 '14 at 22:05
  • 1
    @user3288131 It doesn't matter the version, it's the device, as only some devices support that `ContentProvider`. – hichris123 Feb 08 '14 at 22:08

1 Answers1

1

The content://sms/ is not a supported content provider. It is a hidden method, and may not be available in all devices. In Android 4.4 KitKat devices, you can use the code in this answer to do it:

public List<String> getAllSms() {
  List<String> lstSms = new ArrayList<String>();
  ContentResolver cr = mActivity.getContentResolver();

  Cursor c = cr.query(Telephony.Sms.Inbox.CONTENT_URI, // Official CONTENT_URI from docs
                      new String[] { Telephony.Sms.Inbox.BODY }, // Select body text
                      null,
                      null,
                      Telephony.Sms.Inbox.DEFAULT_SORT_ORDER // Default sort order);
  int totalSMS = c.getCount();

  if (c.moveToFirst()) {
      for (int i = 0; i < totalSMS; i++) {
          lstSms.add(c.getString(0));
          c.moveToNext();
      }
  }
  else {
      throw new RuntimeException("You have no SMS in Inbox"); 
  }
  c.close();

  return lstSms;
}

I don't believe that there is a documented method that will work across all devices right now.

Community
  • 1
  • 1
hichris123
  • 10,145
  • 15
  • 56
  • 70
  • Thank for fast answer. Is there any way to works on both of Android 4.4+ and 4.4- version? – user3288131 Feb 08 '14 at 22:09
  • @user3288131 Not that I know of. Keep in mind that content provider will work on some devices, but not all... but all 4.4 devices will support the above code. For more information, read [this question](http://stackoverflow.com/questions/848728/how-can-i-read-sms-messages-from-the-inbox-programmatically-in-android?lq=1). – hichris123 Feb 08 '14 at 22:11