android - 通过 thread_id 取得联系

标签 android messaging

我在讨论这个话题Android get contact id from thread id有人和我问的问题完全一样。不幸的是,lopez.mikhael 的回答完全没有中标。

如何从 URI content://sms/conversations 中获取带有 thread_id 的联系人 ID?假设它返回:

- thread_id
- snippet
- msg_count

获得所有联系方式无济于事。我从那里搜索了 getColumnsNames(),但没有看到任何包含 thread_id 的好列名。

为什么这没有记录在案?是否已弃用 content://sms/ ?还有其他解决方案吗?

谢谢。

最佳答案

如此处所述post

您可以获取短信收件箱:

    Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
    String where = "thread_id="+ <thread id you get it from content://sms/conversations>;
    Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,new String[] { "_id", "thread_id", "address", "person", "date","body", "type" }, where, 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]));
        }
    }

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

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

如果你有电话号码,你可以使用下面的代码找到联系方式

String contactid = null;

ContentResolver contentResolver = getContentResolver();

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));

Cursor cursor =  contentResolver.query(
                        uri, 
                        new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
                        null, 
                        null, 
                        null);

if(cursor!=null) {
 while(cursor.moveToNext()){
    String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
    contactid = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
 }
 cursor.close();
}
if (contactid == null) {
  Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}else{
 //You can contact id do what you want to do with it.
}

关于android - 通过 thread_id 取得联系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30238447/

相关文章:

android - newMyFriendsRequest Facebook 仅返回 25 个好友

cocoa - 在 openfire 中注册新用户?

java - 为什么我应该使用 ActiveMQ 发送电子邮件?

android消息系统的实现方法

java.net.SocketTimeoutException (安卓)

android - 如何将 ACHARTENGINE 整合到我的 LAYOUT 中?

安卓加速度计全系列

Android 从命令行停止模拟器

multithreading - 使用DefaultConsumer和QueueingConsumer的RabbitMQ Java客户端

apache-flex - 如何防止 RemoteObject 一起批处理 AMF 消息?