我想获取和查看短信对话?

时间:2011-08-12 05:33:52

标签: android

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

无法正常工作?

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

权限。

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)

5 个答案:

答案 0 :(得分:27)

您可以获取短信收件箱:

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]));
    }
}

您可以通过更改URI来获取其他已发送的项目。

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

您也可以使用带有URI的彩信:

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

对于SMS-MMS两者:

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

答案 1 :(得分:16)

终于得到了我需要的东西!

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);

答案 2 :(得分:2)

我不确定,我还没有尝试过,但我认为这可能是你的工作。

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

我之前从这个答案中找到了这个:How to delete an SMS from the inbox in Android programmatically?

答案 3 :(得分:1)

env

答案 4 :(得分:1)

此功能用于提取对话

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();
}