按名称提取联系电话号码

时间:2013-07-30 20:59:44

标签: java android android-contacts

我试图通过他的名字获取联系人的电话号码,(名字是一个字符串),有没有人知道如何? 谢谢!

2 个答案:

答案 0 :(得分:2)

这应该对你有帮助;将People.NAME替换为您想要的名称。

    String[] projection = new String[]{
            People.NAME,
            People.NUMBER
         };

    Cursor c = ctx.getContentResolver().query(People.CONTENT_URI, projection,
    null, null, People.NAME + " ASC");
    c.moveToFirst();
    int nameCol = c.getColumnIndex(People.NAME);
    int numCol = c.getColumnIndex(People.NUMBER);

    int nContacts = c.getCount();
    do{
        // Do your work here
    } while(c.moveToNext());

希望它有所帮助。

答案 1 :(得分:0)

应该适合你。 返回分配给此联系人的所有号码的列表

   public static List<String> getContactByName(Context context,
         String name) {
         String[] mProjection = {
               ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
               ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
               ContactsContract.CommonDataKinds.Phone.NUMBER };

         Cursor cursor = context.getContentResolver().query(
               ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
               mProjection,
               ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE '%"
                     + name + "%'", null, null);

         List<String> listOfPhonesAssignedToThisName = new ArrayList<String>();
         while(cursor.moveToNext()){
            listOfPhonesAssignedToThisName.add(cursor.getString(cursor.getColumnIndexOrThrow("DISPLAY_NAME")));
         }
         return listOfPhonesAssignedToThisName;
   }

注意: 此功能正在比较所有看起来 LIKE 您的显示名称的内容。 如果你想找到恰当的人,你需要调整部分:

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +“LIKE'%”                          + name +“%'”,null,null);

于: ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME +“LIKE'”                          + name +“'”,null,null);