如何通过thread_id获取所有消息的正文?
How to get all message's body by thread_id?
我有一个 activity 显示我的收件箱 这个 activity 有一个列表视图和一个 OnItemClickListener,当我点击每个项目时,一个新的活动开始:我想显示我在这里点击它的联系人是我的代码的一部分 activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation);
ContentResolver contentResolver = getContentResolver();
Bundle extras = getIntent().getExtras();
String value = null;
if (extras != null) {
value = extras.getString("id");
}
Uri uri = Uri.parse("content://sms/conversations/");
Cursor smsCur = contentResolver.query(uri, null, "thread_id=" + value, null, null);
现在我想通过 thread_id 获取我从之前的 activity 收到的所有消息,但我不知道如何?我的问题是如何获取消息正文?关于这个主题有很多问题,但我没有找到关于如何查询邮件正文的内容!
您可以使用以下代码来获取对话。
获取已发送的消息
Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");
获取收件箱消息
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
对于 SMS-MMS 两者:
Uri uri = Uri.parse("content://mms-sms/conversations/");
阅读消息的代码如下
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null);
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]));
}
}
参考资料
我找到了一种获取对话中所有消息的方法 thread_id,因此我只能获取两个消息之间的消息,而不是其他消息:
Uri uri = Uri.parse("content://sms/");
cursor = contentResolver.query(uri, null, "thread_id=" + value, null, "date asc");
有一个名为 "type" 的列显示消息是传入消息还是传出消息,因此传入消息显示属于联系人的对话部分,传出消息显示我的对话部分,我认为这是一个有效的方式。
我有一个 activity 显示我的收件箱 这个 activity 有一个列表视图和一个 OnItemClickListener,当我点击每个项目时,一个新的活动开始:我想显示我在这里点击它的联系人是我的代码的一部分 activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversation);
ContentResolver contentResolver = getContentResolver();
Bundle extras = getIntent().getExtras();
String value = null;
if (extras != null) {
value = extras.getString("id");
}
Uri uri = Uri.parse("content://sms/conversations/");
Cursor smsCur = contentResolver.query(uri, null, "thread_id=" + value, null, null);
现在我想通过 thread_id 获取我从之前的 activity 收到的所有消息,但我不知道如何?我的问题是如何获取消息正文?关于这个主题有很多问题,但我没有找到关于如何查询邮件正文的内容!
您可以使用以下代码来获取对话。
获取已发送的消息
Uri mSmsinboxQueryUri = Uri.parse("content://sms/sent");
获取收件箱消息
Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
对于 SMS-MMS 两者:
Uri uri = Uri.parse("content://mms-sms/conversations/");
阅读消息的代码如下
Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, null, null, null);
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]));
}
}
参考资料
我找到了一种获取对话中所有消息的方法 thread_id,因此我只能获取两个消息之间的消息,而不是其他消息:
Uri uri = Uri.parse("content://sms/");
cursor = contentResolver.query(uri, null, "thread_id=" + value, null, "date asc");
有一个名为 "type" 的列显示消息是传入消息还是传出消息,因此传入消息显示属于联系人的对话部分,传出消息显示我的对话部分,我认为这是一个有效的方式。