Search code examples
androidsmsandroid-contentprovider

Limiting and offsetting query result in SMS Content Provider


I am working on a messaging app, I want to limit the number of result from SMS content provider so that I can implement endless scrolling (loading more data on scroll rather than loading the whole conversation at once).

Is there any way to select the top 50 conversations from SMS Content provider, and later to select 50 conversations from a given point ?

Cursor cur;
ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/");

cur = contentResolver.query(uriSMSURI, projection,
    null, null, " date DESC");

Solution

  • Specify limit 50 in the last argument

    cur = contentResolver.query(uriSMSURI, projection, null, null, " date DESC limit 50");
    

    If you want to get the ith "page", meaning offset by i times 50 :

    ...," date DESC limit 50 offset "+i);
    

    (where I assume i is the variable where you hold the index of the chunk you would like to load)