2013-05-22 28 views
1

我正在处理一个简单的短信应用程序,我使用下面的代码来获取线程ID加载我的线程列表时,但我无法弄清楚如何获得联系人ID使用线程ID。我的根和用root探险家我可以在数据库中看到有一个接触表具有以下的列Android从线程ID获取联系人ID

thread_id | htcthread_id | contact_id

所以,因为我有线程ID,我应该能够获得接触ID,但我还需要确保它适用于所有设备。我的应用程序不是由路根

代码来获取线程ID

Uri uri = Uri.parse("content://mms-sms/conversations?simple=true"); 
Cursor c = context.getContentResolver().query(uri, null, null, null, "date desc"); 
if (c.getCount() > 0) { 
    while (c.moveToNext()){ 
     //thread id is c.getString(c.getColumnIndexOrThrow("_id")) 
    } 
} 
c.close 
+0

是否成功? – Hamidreza

回答

1

我的解决方案,以恢复所有联系人:

Cursor cursor = null; 
    try { 
     cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null); 
     int contactIdIdx = cursor.getColumnIndex(Phone._ID); 
     int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME); 
     int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER); 
     int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID); 
     cursor.moveToFirst(); 
     do { 
      String idContact = cursor.getString(contactIdIdx); 
      String name = cursor.getString(nameIdx); 
      String phoneNumber = cursor.getString(phoneNumberIdx); 
      //... 
     } while (cursor.moveToNext()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 

你需要这个权限在你的清单:

<uses-permission android:name="android.permission.READ_CONTACTS" /> 

我希望我帮你!

+1

+1使用:Phone.CONTENT_URI – cbrulak

+4

好吧,但这怎么回答我的问题?我没有试图获得所有的联系人,我只是想要基于线程ID的联系信息 – user577732

+0

那么接下来呢?答案接受为解决方案? – Sw0ut

相关问题