2013-05-22 29 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

相關問題