Search code examples
androidphone-number

Could not get phone number in Android 2.3.3


I write a simple application to get phone number in Contacts. However, the phone number return "null".

Here is my code:

private void queryContactPhoneNumber() {
    // TODO Auto-generated method stub
    String[] cols = new String[] {People.NAME, People.NUMBER};
    Uri myContacts = People.CONTENT_URI;
    Cursor mqCur =  managedQuery(myContacts, cols, null, null, null);
    if(mqCur.moveToFirst())
    {
        String myname = null;
        String mynumber = null;
        do
        {
            myname = mqCur.getString(mqCur.getColumnIndex(People.NAME));
            mynumber = mqCur.getString(mqCur.getColumnIndex(People.NUMBER));
            Toast.makeText(this, myname + " " + mynumber, Toast.LENGTH_SHORT).show();
        }
        while(mqCur.moveToNext());
    }
}

Solution

  • Try this,

    Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI ;//People.CONTENT_URI;
            Cursor mqCur =  managedQuery(myContacts, null, null, null, null);
            if(mqCur.moveToFirst())
            {
                String myname = null;
                String mynumber = null;
                do
                {
                    myname = mqCur.getString(mqCur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    mynumber = mqCur.getString(mqCur
                            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(this, myname + " " + mynumber, Toast.LENGTH_SHORT).show();
                }
                while(mqCur.moveToNext());
            }
    

    I think this will help you.