how to get the group id of a contact in android - android

I want to get the group id of each contact in my android application.I have initial query like this
Cursor cursor = cr.query(Phone.CONTENT_URI,
new String[] {
Phone.CONTACT_ID}, null, null, null);
And what i do is get details of each which related to this CONTACT_ID.I have manage to get phone number,names,addresses and emails so far but still couldn't manage to get the group id of the contact.
P.S i found a question asked before here but the class android.provider.Contacts.GroupMembership is deprecated.
thanks.

Try this:
final String selection = "mimetype_id = (select _id from mimetypes where mimetype = \"" +
vnd.android.cursor.item / group_membership + "\")";
Cursor cursor = getContentResolver().query(Data.CONTENT_URI,
new String[]{Phone.CONTACT_ID}, selection, null, null);
try {
if (cursor != null && cursor.moveToFirst()) {
do {
Log.i("Details", "Contact IDs" + cursor.getLong(cursor.getColumnIndex(Phone.CONTACT_ID)));
} while (cursor.moveToNext());
}
} finally {
if (cursor != null) {
cursor.close();
}
}

Related

Android: get all contacts by IM

I want to query to Contacts content provider such that if a contact has IM whose type is equal to "XYZ".
I tried below way but I am not getting any result:
Uri uri1 = ContactsContract.Contacts.CONTENT_URI;
String[] projection1 = null;
String selection1 = null;
String[] selectionArgs1 = null;
String sortOrder1 = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
Cursor cursor1 = context.getContentResolver().query(uri1, projection1, selection1, selectionArgs1, sortOrder1);
if (cursor1 != null && cursor1.getCount() > 0) {
while (cursor1.moveToNext()) {
int contactId = Integer.parseInt(cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID)));
Uri uri2 = ContactsContract.Data.CONTENT_URI;
String[] projection2 = null;
String selection2 = ContactsContract.CommonDataKinds.Im.PROTOCOL + " = ? AND " + ContactsContract.Contacts._ID + " = ? ";
String[] selectionArgs2 = new String[]{"XYZ", contactId + ""};
String sortOrder2 = null;
Cursor cursor2 = context.getContentResolver().query(uri2, projection2, selection2, selectionArgs2, sortOrder2);
if (cursor2 != null && cursor2.getCount() > 0) {
while (cursor2.moveToNext()) {
Log.i(TAG, "Name: " + cursor2.getString(cursor2.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
}
DatabaseUtils.dumpCursor(cursor2);
}
}
cursor1.close();
}
I am not getting any log with above code.
PS: I am not using built in protocols like AIM, Windows Live, Yahoo or skype. Its my custom Protocol, say it "XYZ".
For this, you need to query the ContactsContract.Data.CONTENT_URI and with the mime type as IM and then the label or type field (not sure) holds that which type of IM like you said 'XYZ' and in the value column you will get the value like a username.
There is a foreign key in this table which is linked to raw contact id of raw_contacts table.
UPDATE
Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(
ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.MIMETYPE + "=?", new String[]{ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE}, null);
if(cursor!=null) {
cursor.moveToFirst();
do {
String value = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
//Types are defined in CommonDataKinds.Im.*
int imppType = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
//Protocols are defined in CommonDataKinds.Im.*
int imppProtocol = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL));
//and in this protocol you can check your custom value
}while (cursor.moveToNext());
cursor.close();
}
Thanks
I have stumbled upon the same problem. Turns out that the protocol should be an Int instead of a String. In case of being a custom one you should use ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM which is an alias for -1.

Android - Find a contact by display name

I'm trying to find a contact by display name. The goal is to open this contact and add more data to it (specifically more phone numbers), but I'm struggling to even find the contact I want to update.
This is the code I'm using:
public static String findContact(Context context) {
ContentResolver contentResolver = context.getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;
String[] projection = new String[] { PhoneLookup._ID };
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?";
String[] selectionArguments = { "John Johnson" };
Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
if (cursor != null) {
while (cursor.moveToNext()) {
return cursor.getString(0);
}
}
return "John Johnson not found";
}
I do have a contact called "John Johnson", but the method always returns "not found". I also tried searching for a contact with just one name, so that makes no difference.
I suspect that it's something wrong with the uri, selection or selection arguments, because I couldn't find any example online of searching for contacts with a given display name, and it seems display name is a special kind of information, different from for example a phone number.
Any ideas how I can achieve to find John Johnson?
UPDATE: I found out how to find a contact by display name:
ContentResolver contentResolver = context.getContentResolver();
Uri uri = Data.CONTENT_URI;
String[] projection = new String[] { PhoneLookup._ID };
String selection = StructuredName.DISPLAY_NAME + " = ?";
String[] selectionArguments = { "John Johnson" };
Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
if (cursor != null) {
while (cursor.moveToNext()) {
return cursor.getString(0);
}
}
return "John Johnson not found";
This code returns the contact id of the first contact with display name "John Johnson". In my original code I had the wrong uri and the wrong selection in my query.
I thinks the issue may caused by the projection you set. Projection is used to tell android which column of data you want to query then you only give the id column so the display name won't return. Try to remove the projection to see whether it works.
-- Cursor cursor = contentResolver.query(uri, projection, selection, selectionArguments, null);
++ Cursor cursor = contentResolver.query(uri, null, selection, selectionArguments, null);
Change Your Query URI.
You are using a URI that is meant to filter only phones numbers:
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI;
You need to use a URI that has access to the display_name column, like this:
Uri uri = ContactsContract.Data.CONTENT_URI;
There's a decent breakdown of what URIs to use and when to use them on the Android SDK Documentation:
If you need to read an individual contact, consider using CONTENT_LOOKUP_URI instead of CONTENT_URI.
If you need to look up a contact by the phone number, use PhoneLookup.CONTENT_FILTER_URI, which is optimized for this purpose.
If you need to look up a contact by partial name, e.g. to produce filter-as-you-type suggestions, use the CONTENT_FILTER_URI URI.
If you need to look up a contact by some data element like email address, nickname, etc, use a query against the ContactsContract.Data table. The result will contain contact ID, name etc.
//method for gaining id
//this method get a name and make fetch it's id and then send the id to other method //named "showinformation" and that method print information of that contact
public void id_return(String name) {
String id_name=null;
Uri resultUri = ContactsContract.Contacts.CONTENT_URI;
Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME + " = ?" ;
String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,name};
Cursor nameCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur.moveToNext()) {
id_name = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID));}
nameCur.close();
cont.close();
nameCur.close();
//for calling of following method
showinformation(id_name);
}
//method for showing information like name ,phone, email and other thing you want
public void showinformation(String id) {
String name=null;
String phone=null;
String email=null;
Uri resultUri = ContactsContract.Contacts.CONTENT_URI;
Cursor cont = getContentResolver().query(resultUri, null, null, null, null);
String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID+ " = ?" ;
String[] whereNameParams1 = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,id};
Cursor nameCur1 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams1, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur1.moveToNext()) {
name = nameCur1.getString(nameCur1.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME));}
nameCur1.close();
cont.close();
nameCur1.close();
String[] whereNameParams2 = new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,id};
Cursor nameCur2 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams2, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur2.moveToNext()) {
phone = nameCur2.getString(nameCur2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));}
nameCur2.close();
cont.close();
nameCur2.close();
String[] whereNameParams3 = new String[] { ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,id};
Cursor nameCur3 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams3, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
while (nameCur3.moveToNext()) {
email = nameCur3.getString(nameCur3.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));}
nameCur3.close();
cont.close();
nameCur3.close();
String[] whereNameParams4 = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE,id};
Cursor nameCur4 = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams4, ContactsContract.CommonDataKinds.StructuredPostal.DATA);
while (nameCur4.moveToNext()) {
phone = nameCur4.getString(nameCur4.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));}
nameCur4.close();
cont.close();
nameCur4.close();
//showing result
txadd.setText("Name= "+ name+"\nPhone= "+phone+"\nEmail= "+email);
}
//thank all persons in this site because of many help of me to learn and correction my warn and errors this is only a gift for all of you and ...
The below code should do the trick
if (displayName != null) {
Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(displayName));
String[] displayNameProjection = { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri, displayNameProjection, null, null, null);
try {
if (cur.moveToFirst()) {
return true;
}
} finally {
if (cur != null)
cur.close();
}
return false;
} else {
return false;
}
Reference: Retrieving a List of Contacts Article

Display contact names by part of phone number

I have a function that takes a number and seeks for it into the phone contacts, but after that it returns the contact name.
public String[] getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "?";
String[] nameStr = {};
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
try {
while (contactLookup != null && contactLookup.getCount() != 0 && !contactLookup.isLast()) {
contactLookup.moveToNext();
nameStr = insertElement(nameStr,contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
//String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return nameStr;
}
What do I got to modify to make it search for a part of a number?
ex if the number is:0729587347, I want to search for 0729.
If there are many contact starting with that number, I want them all(It is something like MYSQL %variable% search ...)!
What's to do ?
Thanks !
The ContentResolver.query(...) can take a WHERE clause parameter.
Specifically, use something like:
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Phone.NUMBER + " like ?",
new String[] {"%"+String.valueOf(userInput) +"%"}, null);
For more examples, see the ContactsContract.Data javadoc.
Also, this is similar to a previous question.

In Android, how do you select only PHONE contacts?

I am querying the Contacts out of the built in Contacts provider URI in Android. I want to get just the PHONE contacts; is there any consistent way to do this? It seems from what I can find that the account name for phone contacts differs from manufacturer to manufacturer (see this question). Is there a way to get PHONE contacts (not SIM, Facebook, Twitter or others) in a consistent, reliable, manufacturer- and device- agnostic way?
Cursor cursor = null;
try {
String selection = ContactsContract.Data._ID + " = ?";
String[] selectionArgs = new String[] { id };
String[] projection = new String[] { ContactsContract.PhoneLookup.NUMBER};
cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
projection, selection, selectionArgs, null);
if (cursor == null || !cursor.moveToFirst())
return;
String phone = cursor.getString(0);
} finally {
if (cursor != null && !cursor.isClosed())
try {
cursor.close();
} catch (Throwable ignore) {
// Ignored.
}
}
Where "?" is user ID, you can put this code into a loop.

Retrieving a contacts notes

I'm still very new to android app development, and I have hit a problem I hope u can help me with...
I am trying to retrieve any "Notes" stored against a contact within my phone. I want to check if a contact (current caller) has any notes associated to them, and then either display the contents or do some action depending on the content of them etc...
I have tried the code below as a test to see if the data is retrieved anywhere within my cursor, but although it retrieves some data, I can't see the content of a note - so I guess I'm in the wrong place!
Where I have declared contentID, this is a result of doing a lookup with the code below, and using the id that is recieved.
Uri lookupUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(inCommingNumber));
String[] mPhoneNumberProjection = { Phone._ID, Phone.NUMBER, Phone.DISPLAY_NAME};
Cursor cur = getContentResolver().query(lookupUri , mPhoneNumberProjection, null, null, null);
private boolean hasNote(String contactID){
Boolean noteFound = false;
Cursor noteCur = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
if(noteCur.moveToFirst()) {
int numCols = noteCur.getColumnCount();
if(numCols > 0){
for(int x = 0; x < numCols; x++){
Log.d(APP_TAG, " column " + x + " contains: " + noteCur.getString(x));
}
}
noteFound = true;
} else{
Log.d(APP_TAG, "No Note retrieved");
}
noteCur.close();
return noteFound;
}
Apologies, I cant seem to get the code to display properly in this post!!
Any help would be great
I have tried various things, but can't seem to be able to get this data. I have created the note by simply adding it through the normal contact manager. I'm using Android 2.2.
The following code would display the first note for all contacts on your phone:
Cursor contactsCursor = null;
try {
contactsCursor = getContentResolver().query(RawContacts.CONTENT_URI,
new String [] { RawContacts._ID },
null, null, null);
if (contactsCursor != null && contactsCursor.moveToFirst()) {
do {
String rawContactId = contactsCursor.getString(0);
Cursor noteCursor = null;
try {
noteCursor = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Note.NOTE},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Note.CONTENT_ITEM_TYPE + "'",
new String[] {rawContactId}, null);
if (noteCursor != null && noteCursor.moveToFirst()) {
String note = noteCursor.getString(noteCursor.getColumnIndex(Note.NOTE));
Log.d(APP_TAG, "Note: " + note);
}
} finally {
if (noteCursor != null) {
noteCursor.close();
}
}
} while (contactsCursor.moveToNext());
}
} finally {
if (contactsCursor != null) {
contactsCursor.close();
}
}
If you are storing some sort of structured data on the note to take actions on using the free form note field may not be the best approach. With the new contacts contract model you are able to attach your own data fields (in ContactsContract.Data) to a contact just give them your own unique mimetype.

Categories

Resources