content://sms/ 地址列并不总是返回“发件人”数字
当我查询“content://sms/”内容提供商并从地址栏中提取信息时;我总是得到消息“来自”或“发送到”的电话号码。如果我收到一条消息,那么地址就是对方电话号码。当我发送消息时,地址就是我要发送到的消息。
如何区分“content://sms/”文件夹中的消息是已发送消息还是已接收消息,而不查询相应的收件箱/已发送文件夹?
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();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在查询中包含
type
列。它包含一个long,指示您正在处理接收的(type == 1
)还是发送的(type == 2
)消息。这样您就会知道如何解释地址栏。
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.
type 5 似乎是草稿消息类型(至少对于三星 i9100)
type 5 seems to be a draft message type (for samsung i9100 at least)