Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find and collect all the information available in a give contact in any android phone?

Currently I am displaying all the contacts of my phone in my app as a custom recyclerview. Till now I am showing name, mobile number and profile image of the contact on the list item view but I need to get all the information for that contact and display it when the list item of my app is clicked in a custom detail page.

For each contact there is a different set of information available, like in few contacts I have email but for few contacts its not present.

My questions are

  1. How can I get all the information of a given contact without missing a single bit.Is there a structure that I can traverse and check value for each key?

  2. Also when I am populating the system contacts in my apps list, I find same contact multiple times in the list. I think this is due to the fact that in my device's account manager the same number is registered for many accounts like whatsapp, gmail. If so how to display that number only once in my list.

like image 970
Manish Patiyal Avatar asked Mar 17 '16 11:03

Manish Patiyal


People also ask

How do I find information on my Android?

1. For Android 7 : go to "Settings" > "About" > "Hardware information". 2. The screen of "Hardware information" includes the information as below picture.


1 Answers

Here is what you can do: you will have your contact id so base on that you can save details like phone numbers ,emails in your custom object and use that object to display details

Custom class like this:

class Contact
{
    int contactId;
    String Name;
    ArrayList<Phone> phone;
    ArrayList<Email> emails;
}

class Phone
{
    String PhoneType;
    String number;
}

class Email 
{
    String type;
    String EmailId;
}

Retrieve detail and add it to your custom class:

ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
        "DISPLAY_NAME = '" + NAME + "'", null, null);
    if (cursor.moveToFirst()) {
        String contactId =
            cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        //
        //  Get all phone numbers.
        //
        Cursor phones = cr.query(Phone.CONTENT_URI, null,
            Phone.CONTACT_ID + " = " + contactId, null, null);
        while (phones.moveToNext()) {
            String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
            int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Phone.TYPE_HOME:
                    // do something with the Home number here...
                    break;
                case Phone.TYPE_MOBILE:
                    // do something with the Mobile number here...
                    break;
                case Phone.TYPE_WORK:
                    // do something with the Work number here...
                    break;
                }
        }
        phones.close();
        //
        //  Get all email addresses.
        //
        Cursor emails = cr.query(Email.CONTENT_URI, null,
            Email.CONTACT_ID + " = " + contactId, null, null);
        while (emails.moveToNext()) {
            String email = emails.getString(emails.getColumnIndex(Email.DATA));
            int type = emails.getInt(emails.getColumnIndex(Phone.TYPE));
            switch (type) {
                case Email.TYPE_HOME:
                    // do something with the Home email here...
                    break;
                case Email.TYPE_WORK:
                    // do something with the Work email here...
                    break;
            }
        }
        emails.close();
    }
    cursor.close();

Hope this will help

like image 76
KDeogharkar Avatar answered Sep 24 '22 23:09

KDeogharkar