0

我正在尝试在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 块中的一段代码总是崩溃。

4

3 回答 3

3

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

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;
}

它可能不是很有效,但它有效

于 2012-07-10T01:48:23.380 回答
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));
}
于 2012-06-25T11:03:58.837 回答
1

试试这个方法。。

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();
于 2018-04-22T16:18:48.847 回答