androidsmstabletinbox

Android empty sms inbox on Tablet


For a project I have to make an sms application on a tablet with SIM. I am able to send a sms and to receive a sms, but when I use Content Resolver to acces "content://sms/inbox" my cursor has no data. Anyone has an idea of what could be the problem?

Here is my code to read sms from inbox:

private List<Sms> getSms() {
    List<Sms> smsList = new ArrayList<Sms>();
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor c= mContext.getContentResolver().query(uri, null, null ,null,null);
    Log.d("stackoverflow", "Number of sms: " + c.getCount());
    while(c.moveToNext()) {
        Sms sms = new Sms(c.getString(c.getColumnIndexOrThrow("address")).toString(), c.getString(c.getColumnIndexOrThrow("body")).toString());
        smsList.add(sms);
    }
    c.close();
    return smsList;
}

The Log in the code above returns zero, even when I just sent an sms to the Tablet.

Thanks in advance!


Solution

  • From information gleaned in the comments, and the tests you've run on the device, it seems there isn't really a problem. You just started out with a fresh, empty SMS Provider. Since you didn't have a default SMS app installed at the time, there was no app available to write any incoming messages to the inbox, which is why that query came back with no results, even after having received some messages. It seems your app is going to have to handle saving both the outgoing and incoming messages.

    To have write access to the SMS Provider, your app will need to be the default messaging app on the device. Normally, a default app needs to handle a whole slew of things, but you're writing a custom app that needs only to deal with SMS, so you can get away with merely "presenting" it as default-capable. This answer details the minimum requirements for your app to be able to be selected as the default. It's simply a matter of having all the right stuff in the manifest.

    Beyond that, you just need to make the appropriate queries and inserts on the Provider. You've an example query in the question, and a simple example for an insert can be found in this post.

    Should you decide to implement further features of a default app in the future, I'll leave a link to the de facto reference for the changes to the SMS API introduced in KitKat.

    Getting Your SMS Apps Ready for KitKat