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/

相关文章:

java - AlarmManager启动的IntentService不起作用

android - 构建失败并出现错误 : expected reference but got (raw string) #000000

c++ - 分布式进程异步通信的最佳方式是什么?

java - ActiveMQ:如果内存/数据存储已满,则进行故障转移

java - 如何在 Wildfly 中实用地停止 MDB

ios - 在 TableView 中检测平移手势向下滑动

android - 如何在布局代码中轻松访问 anko 组件

android - onLongPress 后继续 onScroll 事件

android - 为什么以及何时应该使用 android Log 类?

java - JMS Receive 如何在内部工作?