开发者

How to get contact email id?

开发者_JAVA技巧I have a listview of all the contact names in the phone. I want to get the email id (if contact have one) of the contact which I click on in the listview. How can I do this?


Use the following code to get all email ids. I checked the code. It is working.

public static void getContactEmails(Context context) {
        String emailIdOfContact = null;
        int emailType = Email.TYPE_WORK;
        String contactName = null;


            ContentResolver cr = context.getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                    null, null, null);
            if (cur.getCount() > 0) {
                while (cur.moveToNext()) {
                    String id = cur.getString(cur
                            .getColumnIndex(BaseColumns._ID));
                    contactName = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    // Log.i(TAG,"....contact name....." +
                    // contactName);

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

                    Cursor emails = cr.query(Email.CONTENT_URI, null,
                            Email.CONTACT_ID + " = " + id, null, null);
                    while (emails.moveToNext()) {
                        emailIdOfContact = emails.getString(emails
                                .getColumnIndex(Email.DATA));
                        // Log.i(TAG,"...COntact Name ...."
                        // + contactName + "...contact Number..."
                        // + emailIdOfContact);
                        emailType = emails.getInt(emails
                                .getColumnIndex(Phone.TYPE));


                    }
                    emails.close();

                }
            }// end of contact name cursor
            cur.close();


    }


Phone Numbers

Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.

if (Integer.parseInt(cur.getString(
               cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            Cursor pCur = cr.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
         new String[]{id}, null);
         while (pCur.moveToNext()) {
         // Do something with phones
         } 
         pCur.close();
     }

Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.

Email Addresses

Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table

Cursor emailCur = cr.query( 
    ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
    null,
    ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", 
    new String[]{id}, null); 
while (emailCur.moveToNext()) { 
    // This would allow you get several email addresses
        // if the email addresses were stored in an array
    String email = emailCur.getString(
                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
     String emailType = emailCur.getString(
                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
 } 
 emailCur.close();


Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,Uri.encode(name.toString().trim()));
Cursor mapContact = getContext().getContentResolver().query(uri, new String[]{PhoneLookup._ID}, null, null, null);
if(mapContact.moveToNext())
{
 String _id = mapContact.getString(mapContact.getColumnIndex(ContactsContract.Contacts._ID));

}


Xamarin Version of Sunil's answer. Took me a while, but I figured it out.

ContentResolver cr = ContentResolver;
        string contactName = null;
        var cur = cr.Query(ContactsContract.Contacts.ContentUri,null,null,null,null);

        if (cur.MoveToFirst())
        {
            do
            {
                string id = cur.GetString(cur.GetColumnIndex(BaseColumns.Id));
                contactName = cur.GetString(cur.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
                var emails = cr.Query(ContactsContract.CommonDataKinds.Email.ContentUri, null, ContactsContract.CommonDataKinds.Email.InterfaceConsts.ContactId + " = " + id, null, null);
                if (emails.MoveToFirst()) {
                    do
                    {
                        // This is where it loops through if there are multiple Email addresses
                        var email = emails.GetString(emails.GetColumnIndex(ContactsContract.CommonDataKinds.Email.InterfaceConsts.Data));
                    } while (emails.MoveToNext());
                }
            } while (cur.MoveToNext());
        }


I am using below code. it is working fine. checked it.

ArrayList<ContactInfo> listContactsData = new ArrayList<>();
   // Retrieve Email address 
                Cursor emailCursor = cr.query(
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (emailCursor.moveToNext()) {
                    // This would allow you get email addresses

                    String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));                              String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
        Log.e(“Email :“,” ”+email)

                    objContact.strEmail = email;
               }
               emailCur.close();

                listContactsData.add(objContact);


 Uri contactData = data.getData();
                Cursor c = getContentResolver().query(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                    String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    String email= "";
                    if (Integer.valueOf(hasNumber) == 1) {
                        Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
                        while (numbers.moveToNext()) {
                            email= numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
                            //Toast.makeText(getApplicationContext(), "Number=" + num, Toast.LENGTH_LONG).show();

                            //asdasdasdsa
                            if(getEmail(email).isEmpty()){
                                Toast.makeText(this, "Email Not Found In That Contact Try Another", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                edt_email_contact.setText("" + getEmail(email));
                            }                            }
                    }
                }
                break;
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜