Android get contact number using name

前端 未结 3 1275
既然无缘
既然无缘 2021-01-16 16:41

I am trying to make an application on android that takes the contact name as a string input and returns his phone number if that contact exists in the phone book...

3条回答
  •  伪装坚强ぢ
    2021-01-16 17:24

    I wrote this method eventually to solve my problem

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

    it may not be very efficient but hey it works

提交回复
热议问题