I want to fetch and view sms conversations?
Asked Answered
P

5

17
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Uri.parse("content://sms/conversations/"), null,null,null, null);     

is not working why?

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

permissions have been added.

08-12 10:56:39.188: ERROR/AndroidRuntime(377): Caused by: android.database.sqlite.SQLiteException: near ",": syntax error: , while compiling: SELECT , body AS snippet FROM sms, (SELECT thread_id AS group_thread_id, MAX(date)AS group_date,COUNT(*) AS msg_count FROM sms GROUP BY thread_id) AS groups WHERE (sms.thread_id = groups.group_thread_id AND sms.date =groups.group_date)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:143)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:111)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:279)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at android.content.ContentProviderProxy.query(ContentProviderNative.java:298)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at android.content.ContentResolver.query(ContentResolver.java:152)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at com.GetMessages.GetConversations$FetchData.doInBackground(GetConversations.java:33)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at com.GetMessages.GetConversations$FetchData.doInBackground(GetConversations.java:1)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-12 10:56:39.188: ERROR/AndroidRuntime(377):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:256)
Pittance answered 12/8, 2011 at 5:33 Comment(0)
P
19

finally got what i needed!

ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, null, null, null);
Pittance answered 30/9, 2011 at 9:26 Comment(0)
O
27

You can fetch sms-inbox:

Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null);
startManagingCursor(cursor1);
String[] columns = new String[] { "address", "person", "date", "body","type" };
if (cursor1.getCount() > 0) {
   String count = Integer.toString(cursor1.getCount());
   while (cursor1.moveToNext()){
       String address = cursor1.getString(cursor1.getColumnIndex(columns[0]));
       String name = cursor1.getString(cursor1.getColumnIndex(columns[1]));
       String date = cursor1.getString(cursor1.getColumnIndex(columns[2]));
       String msg = cursor1.getString(cursor1.getColumnIndex(columns[3]));
       String type = cursor1.getString(cursor1.getColumnIndex(columns[4]));
    }
}

You can fetch other sent items by changing the URI.

Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");

You can do that with MMS also with URI:

RECEIVED_MMS_CONTENT_URI = "content://mms/inbox";
SENT_MMS_CONTENT_URI = "content://mms/sent";

For SMS-MMS both:

Uri uri = Uri.parse("content://mms-sms/conversations/");
Onym answered 30/9, 2011 at 14:12 Comment(1)
How can this be done without using the deprecated startManagingCursor()?Galbanum
P
19

finally got what i needed!

ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, null, null, null);
Pittance answered 30/9, 2011 at 9:26 Comment(0)
G
2

I am not sure and i haven't tried yet, but i think this may be of your work.

Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = getContentResolver().query(uriSms, null,null,null,null); 

I had found this earlier from this answer: How to delete an SMS from the inbox in Android programmatically?

Grethel answered 12/8, 2011 at 6:9 Comment(1)
@but this only shows the received messages not the sent onces..and i need to know why is my query on conversation table not running?as it contatins bothPittance
R
2
String[] projection = {"thread_id", "MAX(date)", "COUNT(*) AS msg_count", "body"};
Cursor cursor = getContentResolver().query(Telephony.Sms.CONTENT_URI, projection, "thread_id) GROUP BY (thread_id", null, null);
Resinoid answered 15/9, 2016 at 10:26 Comment(0)
M
2

This function is for fetching conversations

private void getSMSCOnversationlist() {
    Uri SMS_INBOX = Uri.parse("content://sms/conversations/");
    Cursor c = getActivity().getContentResolver().query(SMS_INBOX, null,null, null, "date desc");

    String[] count = new String[c.getCount()];
    String[] snippet = new String[c.getCount()];
    String[] thread_id = new String[c.getCount()];


    c.moveToFirst();
    for (int i = 0; i < c.getCount(); i++) {

         count[i] = c.getString(c.getColumnIndexOrThrow("msg_count")).toString();
        thread_id[i] = c.getString(c.getColumnIndexOrThrow("thread_id")).toString();
        snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet")).toString();
        Toast.makeText(getActivity(), count[i] + " - " + thread_id[i] + " - " + snippet[i]+ " - " , Toast.LENGTH_LONG).show();
        c.moveToNext();

    }
    c.close();
}
Mangan answered 18/5, 2018 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.