How to get emails from contacts?

Put your problem here if it does not fit any of the other categories.

How to get emails from contacts?

Postby morfeusys » Tue Jan 29, 2008 9:03 pm

Hi!
I am familiarizing with android and have a problem with getting emails from contacts. Can anybody help with code demonstration?
Kind regards!
morfeusys
Freshman
Freshman
 
Posts: 3
Joined: Tue Jan 29, 2008 8:59 pm

Top

Postby plusminus » Wed Jan 30, 2008 12:55 am

Hello morfeusys,

the :src:-Tutorial should help you further.

There choose the right field from the ones provided from People (here: .NAME)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String[] columns = new String[]{People.NAME};
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


Hope I could help you further.
If there are other questions open feel free to ask.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby morfeusys » Wed Jan 30, 2008 9:32 am

Yes, I have seen all tutorials - and this too. But "People" class contains only one column name about email - PREFERRED_EMAIL_ID. It's a foreign key to the other table - "columns_methods" (or something such - I don't remember now). What should I do with this value for getting email string???
Thank you!
morfeusys
Freshman
Freshman
 
Posts: 3
Joined: Tue Jan 29, 2008 8:59 pm

Postby Ishtar » Thu Jan 31, 2008 3:57 pm

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //todo set projection
  2.  
  3. Cursor contactmethods = a.managedQuery(mURIContact.addPath("contact_methods"),null,null,null);
  4.  
  5. if (contactmethods.first())
  6.  
  7.                 {
  8.  
  9.                         int cID=contactmethods.getColumnIndex(android.provider.BaseColumns._ID);
  10.  
  11.                         int cKind = contactmethods.getColumnIndex(android.provider.Contacts.ContactMethodsColumns.KIND);
  12.  
  13.                         int cData = contactmethods.getColumnIndex(android.provider.Contacts.ContactMethodsColumns.DATA);
  14.  
  15.                        
  16.  
  17.                         do
  18.  
  19.                         {
  20.  
  21.                                 switch(contactmethods.getInt(cKind)){
  22.  
  23.                                 case Contacts.ContactMethodsColumns.EMAIL_KIND:
  24.  
  25.                                      Log.v("EMAIL!!", "emailadress=" + contactmethods.getInt(cData));
  26.  
  27.                                      break;
  28.  
  29.                                 }
  30.  
  31.                         }while(contactmethods.next());
  32.  
  33.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Copied from my own source. :) mURIContact is a failed uri of one contact. You can add a where clause to let the id match the preferred email, if you only want that email.

[EDIT]
Sorry for the crappy layout.. i copied it..

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby androooooid » Fri Nov 14, 2008 3:48 am

Ishtar wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //todo set projection
  2. Cursor contactmethods = a.managedQuery(mURIContact.addPath("contact_methods"),null,null,null);
  3. if (contactmethods.first())
  4.                 {
  5.                         int cID=contactmethods.getColumnIndex(android.provider.BaseColumns._ID);
  6.                         int cKind = contactmethods.getColumnIndex(android.provider.Contacts.ContactMethodsColumns.KIND);
  7.                         int cData = contactmethods.getColumnIndex(android.provider.Contacts.ContactMethodsColumns.DATA);
  8.                        
  9.                         do
  10.                         {
  11.                                 switch(contactmethods.getInt(cKind)){
  12.                                 case Contacts.ContactMethodsColumns.EMAIL_KIND:
  13.                                      Log.v("EMAIL!!", "emailadress=" + contactmethods.getInt(cData));
  14.                                      break;
  15.                                 }
  16.                         }while(contactmethods.next());
  17.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Copied from my own source. :) mURIContact is a failed uri of one contact. You can add a where clause to let the id match the preferred email, if you only want that email.

[EDIT]
Sorry for the crappy layout.. i copied it..

Ishtar


this code doesn't get the actual email address out :?:
it does the same thing as the code posted before
androooooid
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Sep 26, 2008 8:47 pm

Postby Goomba » Wed Dec 03, 2008 5:24 pm

Hello friends,

At last I managed to get emails from contacts :D . Here is the code:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                
  2.  
  3.                 Uri mContacts= Contacts.ContactMethods.CONTENT_URI;
  4.  
  5.                
  6.  
  7.                 String[] projection = new String[] {
  8.  
  9.                                 Contacts.ContactMethods.PERSON_ID,
  10.  
  11.                                 Contacts.ContactMethods.KIND,
  12.  
  13.                             Contacts.ContactMethods.DATA
  14.  
  15.                 };
  16.  
  17.                
  18.  
  19.                
  20.  
  21.                 Cursor c = this.managedQuery( mContacts,
  22.  
  23.                                                                 projection,
  24.  
  25.                                                                 null,      
  26.  
  27.                                                                 null,
  28.  
  29.                                                                 Contacts.ContactMethods.PERSON_ID + " ASC");
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


This retrieves the contact's ID and email (inside DATA field). However, I don´t know the aim of the KIND field, in my case always stores the value "1".
Goomba
Freshman
Freshman
 
Posts: 3
Joined: Fri Nov 14, 2008 4:32 pm
Location: Madrid, Spain

Top

Postby kenjkelly » Wed Dec 03, 2008 5:57 pm

Hopefully this will help you on Emails - I'm interested in your end result:

When I saw the concept of Providers, I thought - this is either a terrible idea or it is poorly documents. After painfully figuring out how to get all the phone numbers for a contact, I realized the idea is OK but the documentation of the idea is - well, they should be ashamed.

In my case of phone numbers, getting the contacts person only gets ONE (in my case phone number). I needed them all. So here is what the Uri ended up as:


Use Uri to get to the appropriate table
o Base Uri for contacts: Uri mContacts = People.CONTENT_URI; // which is: content://contacts/people/

o Uri for a single person: Uri person_uri = ContentUris.withAppendedId(People.CONTENT_URI, person_id ); // So now the URL looks like: content://contacts/people/_id(where the actual long id of the record is here) For example: content://contacts/people/23 (this much they explain well)

o Uri for the phone numbers belonging to this person: Uri numbers_uri = Uri.withAppendedPath(person_uri,Contacts.People.Phones.CONTENT_DIRECTORY); // Now the URL looks like: content://contacts/people/_id/phones (where phones is literally the string "phones") For example:
content://contacts/people/23/phones (this took a lot of trial and error)

o Phone number_key is reversed and has a bug where it is not updated if the number is changed.

In your case I suspect the last word on the Uri will be literally "emails" or something. Let me know.

The key to the documentation problem is that they refer to the CONTENT_DIRECTORY as a "leaf" without telling you when or where to use it. Also, the columns you see when you get all data is not defined in one class, but spread out over a series "Columns from the People table that other tables join into themselves". The only way I knew what columns would be available in the phones subdirectory was to get all the data and look at it. They never mention they reverse the number in number_key; isn’t that important enough to document?

End of rant and help.

== EXAMPLE OF GETTING ALL PHONE NUMBERS cut and pasted from cvarious routines I have:

void trythiscode(){
// An array specifying which columns to return.
String[] projection = new String[] {
People._ID,
People.NAME,
People.NUMBER,
};

// Get the base URI for People table in Contacts content provider.
// which is: content://contacts/people/
Uri contactUri = People.CONTENT_URI;

// Best way to retrieve a query; returns a managed query.
Cursor peopleCursor = managedQuery (contactUri,
projection, //Which columns to return.
null, // WHERE clause--we won't specify.
null, // Selection Args??
People.DEFAULT_SORT_ORDER); // Order-by name

// go to the beginning of the list
peopleCursor.moveToFirst();


// So, here we have a contact. We need to get the contact ID (_id) then
// build the Uri to get the phones section of that user's record
// which is a subdirectory of a contact record

long personId = peopleCursor.getLong(peopleCursor.getColumnIndex("_id"));

Uri personUri = ContentUris.withAppendedId(contactUri, personId );

// So now the URL looks like: content://contacts/people/_id(where the actual id of the record is here)
Uri phoneUri=
Uri.withAppendedPath(personUri, Contacts.People.Phones.CONTENT_DIRECTORY);

// Now the URL looks like: content://contacts/people/_id/phones (where phones is literally "phones")

// Now get all the phone numbers for this contact
Cursor phonesCursor = managedQuery(phoneUri,
null,
null,
null,
Phones.DEFAULT_SORT_ORDER);

// We now have a cursor for all the phone numbers for that User ID
// go to the beginning of the phone list.
phonesCursor.moveToFirst();


}
kenjkelly
kenjkelly
Freshman
Freshman
 
Posts: 7
Joined: Mon Oct 06, 2008 3:30 am

Postby umair » Thu Apr 02, 2009 11:52 am

Goomba wrote:Hello friends,

At last I managed to get emails from contacts :D . Here is the code:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                
  2.                 Uri mContacts= Contacts.ContactMethods.CONTENT_URI;
  3.                
  4.                 String[] projection = new String[] {
  5.                                 Contacts.ContactMethods.PERSON_ID,
  6.                                 Contacts.ContactMethods.KIND,
  7.                             Contacts.ContactMethods.DATA
  8.                 };
  9.                
  10.                
  11.                 Cursor c = this.managedQuery( mContacts,
  12.                                                                 projection,
  13.                                                                 null,      
  14.                                                                 null,
  15.                                                                 Contacts.ContactMethods.PERSON_ID + " ASC");
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


This retrieves the contact's ID and email (inside DATA field). However, I don´t know the aim of the KIND field, in my case always stores the value "1".


Thanks for your code. It really helped me.
I think the type tells the type of data. Might be if we have some other information in the contact, the values of type would be different.
Muhammad Umair
User avatar
umair
Developer
Developer
 
Posts: 28
Joined: Thu Feb 26, 2009 1:00 pm
Location: Lahore, Pakistan

Postby ssaidwho » Mon Oct 12, 2009 6:41 am

Thank you,

These posts were very helpful. Here is my code to get email and phone number, roughly, after going through this and digging a little into the docs. In my real code I put the contactCursor into a ListAdapter, presenting contact's name, and pull information from the user's selection (example of this at http://almondmendoza.com/2008/12/25/dis ... n-android/). In the example below, I just get the first contact available.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         String email;
  3.  
  4.         String phone;
  5.  
  6.  
  7.  
  8.         // documentation suggests using projections to avoid pulling all data,
  9.  
  10.         // so get only what is needed
  11.  
  12.         String[] phoneProjection = new String[] { Contacts.Phones.NUMBER,
  13.  
  14.                 Contacts.Phones.PERSON_ID };
  15.  
  16.         String[] emailProjection = new String[] {
  17.  
  18.                 Contacts.ContactMethods.PERSON_ID,
  19.  
  20.                 Contacts.ContactMethods.KIND, Contacts.ContactMethods.DATA };
  21.  
  22.         String[] personProjection = new String[] { People.NAME, People._ID };
  23.  
  24.  
  25.  
  26.         // Set up the cursors
  27.  
  28.         Cursor contactPhoneCursor = getContentResolver().query(
  29.  
  30.                 Contacts.Phones.CONTENT_URI, phoneProjection, null, null, null);
  31.  
  32.         Cursor contactEmailCursor = getContentResolver().query(
  33.  
  34.                 Contacts.ContactMethods.CONTENT_URI, emailProjection, null,
  35.  
  36.                 null, null);
  37.  
  38.         Cursor contactCursor = getContentResolver().query(People.CONTENT_URI,
  39.  
  40.                 personProjection, null, null, People.NAME + " ASC");
  41.  
  42.  
  43.  
  44.         // Let the activity manage all cursors
  45.  
  46.         startManagingCursor(contactCursor);
  47.  
  48.         startManagingCursor(contactPhoneCursor);
  49.  
  50.         startManagingCursor(contactEmailCursor);
  51.  
  52.  
  53.  
  54.         // Now lets pick a person from contactCursor
  55.  
  56.         if (contactCursor.moveToFirst()) {
  57.  
  58.             long personId = contactCursor.getLong(
  59.  
  60.                     contactCursor.getColumnIndex(People._ID));
  61.  
  62.  
  63.  
  64.             // get an email if there is one
  65.  
  66.             if (contactEmailCursor.moveToFirst()) {
  67.  
  68.                 while (!contactEmailCursor.isAfterLast()) {
  69.  
  70.                     long emailPersonId = contactEmailCursor.getLong(
  71.  
  72.                             contactEmailCursor.getColumnIndex(Contacts.ContactMethods.PERSON_ID));
  73.  
  74.                     int myKind = contactEmailCursor.getInt(contactEmailCursor.getColumnIndex(
  75.  
  76.                             Contacts.ContactMethods.KIND));
  77.  
  78.                     if (emailPersonId == personId && Contacts.KIND_EMAIL == myKind) {
  79.  
  80.                         email = contactEmailCursor.getString(
  81.  
  82.                                 contactEmailCursor.getColumnIndex(Contacts.ContactMethods.DATA));
  83.  
  84.                         break;
  85.  
  86.                     }
  87.  
  88.  
  89.  
  90.                     contactEmailCursor.moveToNext();
  91.  
  92.                 }
  93.  
  94.             }
  95.  
  96.  
  97.  
  98.             // get a phone number if there is one
  99.  
  100.             if (contactPhoneCursor.moveToFirst()) {
  101.  
  102.                 while (!contactPhoneCursor.isAfterLast()) {
  103.  
  104.                     long phonePersonId = contactPhoneCursor.getLong(
  105.  
  106.                             contactPhoneCursor.getColumnIndex(Contacts.Phones.PERSON_ID));
  107.  
  108.                     if (phonePersonId == personId) {
  109.  
  110.                         phone = contactPhoneCursor.getString(contactPhoneCursor.getColumnIndex(
  111.  
  112.                                 Contacts.Phones.NUMBER));
  113.  
  114.                         break;
  115.  
  116.                     }
  117.  
  118.                     contactPhoneCursor.moveToNext();
  119.  
  120.                 }
  121.  
  122.             }
  123.  
  124.         }
  125.  
  126.     }
  127.  
  128.  
Parsed in 0.044 seconds, using GeSHi 1.0.8.4
ssaidwho
Freshman
Freshman
 
Posts: 2
Joined: Fri Aug 28, 2009 11:36 pm

new API support

Postby ssaidwho » Mon Jan 25, 2010 9:44 pm

ssaidwho
Freshman
Freshman
 
Posts: 2
Joined: Fri Aug 28, 2009 11:36 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 24 guests