Hi I am working in Android Contact search module.I am running below Query.
cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);
from this query I am getting Result Multiple times.Is there any thing which I am doing wrong.I want DISTINCT Result Set.
please help me.
I think you mean you got duplicate record for some contacts. So you must add condition for your query
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
Try this code will help you
public void getContact() {
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
int size = cur.getCount();
if (size > 0 && cur != null) {
for (int i = 0; i < size; i++) {
cur.moveToPosition(i);
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
Cursor phoneCur = contect_resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (!name.equalsIgnoreCase("")) {
String id1 = phoneCur.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Cursor emails = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + Integer.parseInt(id1),
null, null);
emailAddress="";
if (emails!=null && emails.getCount() > 0) {
emails.moveToFirst();
emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emails.close();
contact.setEmail(emailAddress);
id1 = "";
mcontact_arraylist.add(contact);
}
phoneCur.close();
}
}
cur.close();
}
}
Each record should contain a portion of the data for a contact (eg each phone number or address is a separate row) each row has a mimetype associated with it that is used to determine the data stored in each column. So for an address, the "data1" column holds the street data and data4 might hold the state.
Related
I already saw other similar questions and none worked.
With the code below I was able to get the default phone number of a contact but not all of them.
Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
contact.moveToFirst();
phoneNumberList.add(contact.getString(contact.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));
Then I tried to create multiple cursors with more details about the phone type and then add them to a list:
Cursor contact = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " + ContactsContract.CommonDataKinds.Phone.TYPE + " = " + ContactsContract.CommonDataKinds.Phone.TYPE_HOME, new String[]{contactID}, null);
But unfortunately, it didn't solve anything. Would be thankful if someone helps me with this problem.
I'm assuming you want to get all phone numbers with their type (home, mobile, main, etc.).
Here's how to do it (the list of phones will be printed to log=:
String projection = new String[] { Phone.NUMBER, Phone.TYPE, Phone.LABEL };
String selection = Phone.CONTACT_ID + "=" + contactId;
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
String number = cur.getString(0);
int type = cur.getInt(1); // home / office / personal
String label = cur.getString(2); // a custom label in case type is "TYPE_CUSTOM"
String labelStr = Phone.getTypeLabel(getResources(), type, label);
Log.d(TAG, "got a phone: " + number + " (" + labelStr + ")");
}
if (cur != null) {
cur.close();
}
DEBUG VERSION
String projection = new String[] { Phone._ID, Phone.CONTACT_ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL };
String selection = Phone.CONTACT_ID + "=" + contactId;
Cursor cur = getContentResolver().query(Phone.CONTENT_URI, projection, selection, null, null);
if (cur != null) {
DatabaseUtils.dumpCursor(cur);
cur.close();
}
I need to set the cursor to a specific contact that i have its id
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null,null, null, null);
I have that code for all the database, how can I optimize it?
I need the cursor to return the name and the default number of the contact
thank you
You actually need two cursors. One for name & id and another one for phone number.
Cursor c = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
fullName = c.getString(c.getColumnIndex("display_name_alt"));
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex
(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
//phone number
Cursor phones = getContentResolver().query(Phone.CONTENT_URI,
null,
Phone.CONTACT_ID + " = " + id,
null,
null);
while (phones.moveToNext()) {
String number_type = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.TYPE));
if(number_type.equalsIgnoreCase("1"))
number1 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
if(number_type.equalsIgnoreCase("2"))
number2 = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
}
c.close();
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
Phone.DISPLAY_NAME + " ASC");
I would like to query on phonenumber to obtain the rawcontactID.
The only thing I know of the contact is the given phonenumber, but for my function I need to have the rawcontactID. I got a working code but now I did use 2 seperate queries. What I would like to have is 1 query that can do both just to save some query time.
my code:
Uri uri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] columns = new String[]{Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER, Phone._ID };
Cursor cursor = contentResolver.query(uri, columns, null, null, null);
if(cursor!=null) {
int clenght = cursor.getCount();
while(cursor.moveToNext()){
//contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
id = cursor.getString(cursor.getColumnIndex(Phone.CONTACT_ID));
}
cursor.close();
}
Cursor pCur = contentResolver.query(ContactsContract.Data.CONTENT_URI, new String[]{ContactsContract.Data.RAW_CONTACT_ID}, ContactsContract.Data.CONTACT_ID+" = "+ id, null, null);
if(pCur!=null) {
int clenght = pCur.getCount();
while(pCur.moveToNext()){
//contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
id = pCur.getString(pCur.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
}
pCur.close();
}
thanks in advance
Edit:
My code above works fine, but I am still looking for increasing speed for large number of contacts. Therefore I will give a bounty if someone comes with a solution to combine my queries.
private String[] getRawContactIdFromNumber(String givenNumber){
List<String> rawIds = new ArrayList<String>();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID},ContactsContract.CommonDataKinds.Phone.NUMBER + "='"+ givenNumber +"'",null, ContactsContract.CommonDataKinds.Phone.NUMBER);
while (phones.moveToNext())
{
rawIds.add( phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID)));
Log.v("contacts","Given Number: " + givenNumber + "Raw ID: " +rawIds.get(rawIds.size() - 1));
}
phones.close();
String[] ret = new String[0];
return rawIds.toArray(ret);
}
Edited to only include the raw id in the cursor for efficiency. Also changed return type to array in case multiple contacts have the same number.
Please try
String phonenumber = "input your phone number";
Cursor pCur = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[] { ContactsContract.Data.RAW_CONTACT_ID,
Phone.CONTACT_ID }, Phone.NUMBER + " = " + phonenumber,
null, null);
if (pCur != null) {
while (pCur.moveToNext()) {
String contactID = pCur.getString(pCur
.getColumnIndex(Phone.CONTACT_ID));
String Rowid = pCur.getString(pCur
.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
Log.e("RAW_CONTACT_ID", Rowid);
Log.e("CONTACT_ID", contactID);
}
pCur.close();
}
Now you can get Both CONTACT_ID & RAW_CONTACT_ID in single query.
I am working on an android application for which I need to match the birthday of each contact against current date and if positive, process some business logic, which needs the complete contact details.
I have found ways to read birthdays of contacts or the contacts themselves separately, but am confused as to how to combine both. Can somebody please provide some direction.
Thanks
Found the answer after some looking out on the web. The way this has to be done is :
Get list of contacts
For each contact, get contactId
Get birthday using the contactid
Following is the code snippet :
ContentResolver cr = getContentResolver(); //getContnetResolver()
String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
while (cur.moveToNext()) {
Map<String, String> contactInfoMap = new HashMap<String, String>();
String contactId = cur.getString(cur.getColumnIndex(ContactsContract.Data._ID));
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
String columns[] = {
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.MIMETYPE,
};
String where = Event.TYPE + "=" + Event.TYPE_BIRTHDAY +
" and " + Event.MIMETYPE + " = '" + Event.CONTENT_ITEM_TYPE + "' and " + ContactsContract.Data.CONTACT_ID + " = " + contactId;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;
Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where, selectionArgs, sortOrder);
if (birthdayCur.getCount() > 0) {
while (birthdayCur.moveToNext()) {
String birthday = birthdayCur.getString(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
}
}
birthdayCur.close();
}
cur.close();
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;
}