简体   繁体   中英

How to retrieve “ME” contact details coming in Contact list of Android phone

I am developing an app,which will use phoneOwner details(like name, phoneNo, Email Id etc) which is coming under "ME" header of Contact List. Is there any way to get all these details?

This code will retrive all contactDetails of all contact Inside ContactList

public class MainActivity extends Activity {
    TextView textDetail;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textDetail = (TextView) findViewById(R.id.textView1);
        readContacts();
    }

    public void readContacts() {

        StringBuffer sb = new StringBuffer();

        sb.append("your contacts");

        ContentResolver content = getContentResolver();

        Cursor c = content.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
        String phone = null;
        String emailContact = null;
        String emailType = null;
                String name = null;

        if (c.getCount() > 0) {

            while (c.moveToNext()) {

                String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

                    name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer.parseInt(c.getString(c
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                    System.out.println("ContactName : " + name + ", ID : " + id);

                    sb.append("\n Contact Name:" + name);

                    Cursor phoneno = content.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?", new String[] { id }, null);

                    while (phoneno.moveToNext()) {
                        phone = phoneno.getString(phoneno.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        sb.append("\n Phone number:" + phone);
                        System.out.println("phone" + phone);
                    }
                    phoneno.close();

                    Cursor emailid = content.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID
                                    + " = ?", new String[] { id }, null);

                    while (emailid.moveToNext()) {

                        emailContact = emailid.getString(emailid.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        emailType = emailid.getString(emailid.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                        sb.append("\nEmail:" + emailContact + "Email type:"
                                + emailType);
                        System.out.println("Email " + emailContact
                                + " Email Type : " + emailType);
                    }
                    emailid.close();
                }

                sb.append("\n");
                sb.append("Next");
            }
            textDetail.setText(sb);
        }
    }
}

To retrive only one contact add line as below,

if(name.equals("ME"){

// whatever you want to do
}

It will display only that contact detail..

Cursor c = activity.getContentResolver().query(
                    ContactsContract.Profile.CONTENT_URI, null,  null, null, null);
    int count = c.getCount();
    String[] columnNames = c.getColumnNames();
    c.moveToFirst();
    int position = c.getPosition();
    if (count == 1 && position == 0) {
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < columnNames.length; j++) {
                String columnName = columnNames[j];
                System.out.println(">>"+columnName+" = "+cursorContacts.getString(cursorContacts.getColumnIndex(columnName)));
            }
           c.moveToNext();
        }
    }
    c.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM