Android使用名称获取联系号码

时间:2012-06-25 10:58:12

标签: android

我正在尝试在android上创建一个应用程序,它将联系人姓名作为字符串输入,如果电话簿中存在该联系人,则返回他的电话号码...

我试过四处寻找,但没有明确的教程如何做到这一点

输入:联系人姓名 输出:电话号码

请帮助

      Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 


     while (cursor.moveToNext()) { 
         String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

         if(name.equalsIgnoreCase(token3)) {

        try{     ContentResolver cr = context.getContentResolver();
             Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { ContactsContract.CommonDataKinds.Phone._ID}, null);
             String lname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

         Toast.makeText(context, "number is "+lname, Toast.LENGTH_LONG).show();
        }catch (Exception e) {
            // TODO: handle exception
        }

         }
           } 

这是我到目前为止所拥有的。 try catch块中的代码段总是崩溃。

3 个答案:

答案 0 :(得分:2)

试试这种方式......

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,);
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(name.equals(Your_String)) {
    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
    String lname = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}

答案 1 :(得分:2)

我最终写了这个方法来解决我的问题

public String get_Number(String name,Context context)

{String number="";


Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER};

Cursor people = context.getContentResolver().query(uri, projection, null, null, null);

int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

people.moveToFirst();
do {
    String Name   = people.getString(indexName);
    String Number = people.getString(indexNumber);
    if(Name.equalsIgnoreCase(name)){return Number.replace("-", "");}
    // Do work...
} while (people.moveToNext());


if(!number.equalsIgnoreCase("")){return number.replace("-", "");}
else return number;
}

它可能效率不高但是它有效

答案 2 :(得分:0)

试试这种方式..

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER};

            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);

            int idxName = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            int idxNumber = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            if(cursor.moveToFirst()) {
                do {
                    contactname   = cursor.getString(idxName);
                    contactNumber = cursor.getString(idxNumber);

                    if (contactname.equals("YOUR CONTACT NAME")){
                        Log.d(LOG_TAG,"Contact Name -> "+ contactname +" Contact Number -> "+ contactNumber);
                    }
                } while (cursor.moveToNext());
            }
            cursor.close();