Lee De Blade
Lee De Blade

Reputation: 63

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

Upvotes: 1

Views: 672

Answers (1)

Satheeshkumar
Satheeshkumar

Reputation: 452

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.

Upvotes: 2

Related Questions