我开发了简单的短信/彩信客户端。使用短信,一切正常,但是使用彩信,我遇到了与对话中的地址字段有关的问题。
我有下一种加载对话的方法。最后有短信和彩信。
public static List<Conversation> getConversations(Context c) {
List<Conversation> conversations = new ArrayList<>();
Conversation conversation;
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor cursor = c.getContentResolver().query(uri, null, null, null, "normalized_date DESC");
if (cursor.moveToFirst()) {
for (int i = 0; i < cursor.getCount(); i++) {
conversation = new Conversation();
conversation.setId(cursor.getString(cursor.getColumnIndexOrThrow("_id")));
conversation.setThreadID(cursor.getString(cursor.getColumnIndexOrThrow("thread_id")));
conversation.setDate(new Date(Long.valueOf(cursor.getString(cursor.getColumnIndexOrThrow("date")))));
conversation.setReadType(ReadType.values()[Integer.parseInt(cursor.getString(cursor.getColumnIndexOrThrow("read")))]);
String type = cursor.getString(cursor.getColumnIndexOrThrow("type"));
if (isSMS(type)) {
conversation.setBody(cursor.getString(cursor.getColumnIndexOrThrow("body")));
conversation.setNumber(cursor.getString(cursor.getColumnIndexOrThrow("address")));
conversation.setMessageFromType(MessageFromType.values()[Integer.parseInt(type) - 1]);
} else {
Map<String, String> mmsContent = getMMSByID(c, conversation.getId());
conversation.setBody(mmsContent.get("body"));
conversation.setNumber(mmsContent.get("address"));
}
conversations.add(conversation);
cursor.moveToNext();
}
}
cursor.close();
return conversations;
}
问题是,当我发送彩信时,我的电话号码被放到对话的地址栏中。有了短信,一切都很好。所以之后我不知道谁是我聊天的对手。
我也以下面的方式加载彩信号码
private String getNumber(Context c, String mmsdid) {
String add = "";
final String[] projection = new String[]{"address", "contact_id"};
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(String.valueOf(mmsid)).appendPath("addr");
Cursor cursor = c.getContentResolver().query(
builder.build(),
projection,
null,
null, null);
if (cursor.moveToFirst()) {
add = cursor.getString(cursor.getColumnIndex("address"));
}
return add;
}
也许有人有同样的问题?或对如何解决有什么建议?
最佳答案
答案很简单,希望对您有所帮助。除了一部分以外,其他一切都很好。在选择参数中,我应该添加
“来自= 151”
要么
“来自=” + PduHeaders.TO
因此代码将如下所示:
private static String getNumber(Context c, String id) {
String add = "";
final String[] projection = new String[]{"address"};
Uri.Builder builder = Uri.parse("content://mms").buildUpon();
builder.appendPath(String.valueOf(id)).appendPath("addr");
Cursor cursor = c.getContentResolver().query(
builder.build(),
projection,
"type="+PduHeaders.TO,
null, null);
if (cursor.moveToFirst()) {
add = cursor.getString(cursor.getColumnIndex("address"));
}
cursor.close();
return add;
}
关于android - 我在android对话的彩信地址字段中的电话号码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35895327/