2012-08-16 94 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

相关问题