How to get a contact's number from contact name in android

后端 未结 3 1836
攒了一身酷
攒了一身酷 2020-12-06 06:16

I am having a contacts name with me and want his number .How to get contact number of corresponding name in Android ?

3条回答
  •  [愿得一人]
    2020-12-06 07:03

    Try this code

            people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, "person_name"+"='"+name+"'", null, null);
            people.moveToFirst();  
             {
    
                {  
    
                    try{
                        String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
                        String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                        if ( hasPhone.equalsIgnoreCase("1"))
                            hasPhone = "true";
                        else
                            hasPhone = "false" ;
                        if (Boolean.parseBoolean(hasPhone)) 
                        {
                            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                            while (phones.moveToNext()) 
                            {
                                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                mConno.add(position,phoneNumber);
    
                            }
                            phones.close(); 
                        }   
                        if(hasPhone=="false")
                        {   mConname.remove(position);
                        }   
                        else
                            position++;
                    }       
                    catch(Exception e)
                    { 
    
                    }
                }
    
            }
    

提交回复
热议问题