Created
January 2, 2012 21:44
-
-
Save ermau/1552248 to your computer and use it in GitHub Desktop.
Get a list of names, phones and emails on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TextView t = FindViewById<TextView> (Resource.Id.status); | |
StringBuilder builder = new StringBuilder(); | |
ICursor ncursor = null; | |
try | |
{ | |
ncursor = ContentResolver.Query (ContactsContract.Data.ContentUri, new[] { ContactsContract.DataColumns.Mimetype, ContactsContract.ContactsColumns.LookupKey, ContactsContract.ContactsColumns.DisplayName }, | |
ContactsContract.DataColumns.Mimetype + "=? AND " + ContactsContract.CommonDataKinds.StructuredName.GivenName + "=?", | |
new[] { ContactsContract.CommonDataKinds.StructuredName.ContentItemType, "Eric" }, null); | |
while (ncursor.MoveToNext()) | |
{ | |
string lookupKey = ncursor.GetString (ncursor.GetColumnIndex (ContactsContract.ContactsColumns.LookupKey)); | |
ICursor dcursor = null; | |
try | |
{ | |
dcursor = ContentResolver.Query (ContactsContract.Data.ContentUri, | |
new[] { ContactsContract.DataColumns.Mimetype, ContactsContract.CommonDataKinds.Phone.Number, ContactsContract.DataColumns.Data1 }, | |
ContactsContract.ContactsColumns.LookupKey + "=?", new[] { lookupKey }, null); | |
builder.AppendLine (ncursor.GetString (ncursor.GetColumnIndex (ContactsContract.ContactsColumns.DisplayName))); | |
while (dcursor.MoveToNext()) | |
{ | |
string type = dcursor.GetString (dcursor.GetColumnIndex (ContactsContract.DataColumns.Mimetype)); | |
switch (type) | |
{ | |
case ContactsContract.CommonDataKinds.Phone.ContentItemType: | |
builder.AppendLine ("Phone: " + dcursor.GetString (dcursor.GetColumnIndex (ContactsContract.CommonDataKinds.Phone.Number))); | |
break; | |
case ContactsContract.CommonDataKinds.Email.ContentItemType: | |
builder.AppendLine ("Email: " + dcursor.GetString (dcursor.GetColumnIndex (ContactsContract.DataColumns.Data1))); | |
break; | |
} | |
} | |
builder.AppendLine(); | |
} | |
finally | |
{ | |
if (dcursor != null) | |
dcursor.Close(); | |
} | |
} | |
} | |
finally | |
{ | |
if (ncursor != null) | |
ncursor.Close(); | |
} | |
t.Text = builder.ToString(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment