content://sms/ address column not always return “s

2019-06-08 00:59发布

问题:

When I query the "content://sms/" content provider and pull information from the address column; I always get the phone number that the message is "from" or "sent to". If I receive a message, then address is the number from the other person's phone. When I send a message, then address is the message I am sending to.

How do I differentiate if a message in "content://sms/" folder is a sent message or received message without querying the respective inbox/sent folders?

    Uri uri = Uri.parse("content://sms/");
    String[] columns = new String[] { "_id", "thread_id", "address", "person", "date", "body" };
    String selection = "thread_id = " + threadId;
    String sortOrder = "date DESC";
    String limit = "LIMIT " + String.valueOf(mItemsOnPage);

    TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
    String deviceNumber = tm.getLine1Number();

    Cursor cursor = getContentResolver().query(uri, columns, selection, null,
            sortOrder + " " + limit);

    if (cursor != null) {
        cursor.moveToLast();

        while (!cursor.isBeforeFirst()) {
            long messageId = cursor.getLong(0);
            String address = cursor.getString(2);
            long date = cursor.getLong(4);
            String body = cursor.getString(5);
            long person = cursor.getLong(3);
            cursor.moveToPrevious();
        }
    }
    cursor.close();

回答1:

You need to include the column type in your query. It contains a long indicating whether you are dealing with a received (type == 1) or sent (type == 2) message.

This way you'll know how to interpret the address column.



回答2:

public static final Uri SMS_Inbox = Uri.parse("content://sms/inbox");
public static final Uri SMS_Sent = Uri.parse("content://sms/sent");
public static final Uri SMS_draft = Uri.parse("content://sms/draft");
public static final Uri SMS_Queued = Uri.parse("content://sms/queued");
public static final Uri SMS_ALL = Uri.parse("content://sms/");

public static final int INBOX = 1;
public static final int SEND = 2;
public static final int DRAFT = 3;
public static final int QUEUED = 6;


回答3:

type 5 seems to be a draft message type (for samsung i9100 at least)



标签: android sms