2012-08-16 77 views
0

我希望能夠提取當前存儲在設備默認文本消息應用程序中的文本消息(短信)會話的聯繫人的聯繫信息(姓名和號碼)。做這個的最好方式是什麼?從消息應用程序獲取當前聯繫人

+0

通過對話迭代,拉出名字,然後找到他們的聯繫? – Eric 2012-08-16 17:30:43

+0

是的,這正是我想要做的 – Peter 2012-08-16 17:31:15

回答

1

你可能想查詢以下URI來獲取它們在短信尋址的所有地址和MMS

content://mms-sms/conversations 

你需要得到address

ContentResolver contentResolver = getContentResolver(); 
    final String[] projection = new String[]{"*"}; 
    Uri uri = Uri.parse("content://mms-sms/conversations/"); 
    Cursor query = contentResolver.query(uri, projection, null, null, null); 
    String phone = ""; 
    while(query.moveToNext()){ 
    phone = query.getString(query.getColumnIndex("address")); 
    Log.d("test",phone); 
    } 

編輯: 可以檢查低於從here複製的功能。通過上面的地址欄中挑選數此功能

private String getContactNameFromNumber(String number) { 
    // define the columns I want the query to return 
    String[] projection = new String[] { 
      Contacts.Phones.DISPLAY_NAME, 
      Contacts.Phones.NUMBER }; 

    // encode the phone number and build the filter URI 
    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number)); 

    // query time 
    Cursor c = getContentResolver().query(contactUri, projection, null, 
      null, null); 

    // if the query returns 1 or more results 
    // return the first result 
    if (c.moveToFirst()) { 
     String name = c.getString(c 
       .getColumnIndex(Contacts.Phones.DISPLAY_NAME)); 
     c.close(); 
     return name; 
    } 
    c.close(); 
    // return the original number if no match was found 
    return number; 
} 

你必須編輯這個,因爲調用此爲每個號碼將是緩慢的查詢相匹配。

+0

你知道我如何做到這一點的例子嗎? – Peter 2012-08-16 18:19:09

+0

檢查編輯。 – nandeesh 2012-08-16 18:25:19

+0

謝謝,但我在哪裏可以獲得存儲在手機中的短信或彩信的聯繫人的號碼? – Peter 2012-08-16 18:30:12

相關問題