Search Contact By Name - android

I am trying to create a Custom Contact app which displays only those contacts that have Contact Number. First of all, is there any automated way to do it? Suppose not, then I am trying to search a contact by its name e.g. Rohan.
Here is the code :-
Cursor photoCursor = getContentResolver().query(
android.provider.ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.DISPLAY_NAME },
ContactsContract.Contacts.DISPLAY_NAME + " = ?",
new String[]{"Rohan"}, null);
photoCursor.moveToFirst();
while (photoCursor.moveToNext()) {
Log.d("Photo Thumbnail", "" + photoCursor.getString(1));
}
Although the contact exists, I am not getting any Log, if I remove Selection & Selection Args I see Rohan in the log. What am I doing wrong?

Simple Solution for Searching Partial Display Name.
ContentResolver contentResolver = getCurrentActivity().getContentResolver();
String whereString = "display_name LIKE ?";
String[] whereParams = new String[]{ "%" + searchText + "%" };
Cursor contactCursor = contentResolver.query(
ContactsContract.Data.CONTENT_URI,
null,
whereString,
whereParams,
null );
while( contactCursor.moveToNext() ) {
int contactId = getIntFromCursor( contactCursor, ContactsContract.Data.CONTACT_ID );
Log.d( "Contact ID", contactId)
}
contactCursor.close();

I did it by using the following code
Cursor cursor = getContentResolver().query(
android.provider.ContactsContract.Contacts.CONTENT_URI,
new String[] { ContactsContract.Contacts.PHOTO_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID },
ContactsContract.Contacts.HAS_PHONE_NUMBER, null,
ContactsContract.Contacts.DISPLAY_NAME);
This cursor gives all the contacts that have any phone number and then i save the unique ID in an ArrayList like this
cursor.moveToFirst();
while (cursor.moveToNext()) {
contactsID.add(cursor.getString(2));
}
then on selecting the contact i find the contact numbers using this
Cursor cursor = getContentResolver()
.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME },
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { contactsID.get(position) }, null);
contactNumbers = new ArrayList<String>();
while (cursor.moveToNext()) {
contactNumbers.add(cursor.getString(0));
Log.d("number", cursor.getString(0));
}

Try this:
Cursor contactLookupCursor =
getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode("Rohan")),
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER},
null,
null,
null);
try {
while (contactLookupCursor.moveToNext()) {
contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactNumber = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.NUMBER));
}
} finally {
contactLookupCursor.close();
}

It looks like you are trying to implement a screen that will allow the user to select a contact, and then select a phone number of that contact.
If that's the case, you can use a phone-picker intent instead:
Intent intent = Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
This will open the native Contacts app, and allow the user to select a contact, and a phone number.
You'll then receive the result in your app like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
}
}

Related

Android Contact get Photo Thumbnail in one query

I had many query and it was taking lot of time to fetch contact data.
So need to convert all query in into one.
I got all other info like display name, mobile number, label, email in one query
but finding prob only with getting pics
Help me to Convert below photo query into single query and get uri of pics :
ContentResolver KntRslverVar = getContentResolver();
Cursor ContctKsrVar = KntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER},
null, null, null);
while (ContctKsrVar.moveToNext())
{
String ContctUidVar = ContctKsrVar.getString(ContctKsrVar.getColumnIndex(ContactsContract.Contacts._ID));
String ContctNamVar = ContctKsrVar.getString(ContctKsrVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String ContctMobVar = ContctKsrVar.getString(ContctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Cursor ContctPflPicKsrVar = KntRslverVar.query(
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?",
new String[]{ContctUidVar, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE},
null);
if (ContctPflPicKsrVar != null && ContctPflPicKsrVar.getCount() > 0)
{
while(ContctPflPicKsrVar.moveToNext())
{
Uri ContctPflPicUriVar = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(ContctUidVar));
PflPicUriVar = Uri.withAppendedPath(ContctPflPicUriVar, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
ContctPflPicKsrVar.close();
}
}
Try 1 :
Cursor ContctKsrVar = KntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE},
null, null, null);
Try 2 :
Cursor ContctKsrVar = KntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI},
null, null, null);
Also need help on getting uri from fetched query :
Uri KctPflPicUriVar = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(KctUidVar));
PflPicUriVar = Uri.withAppendedPath(KctPflPicUriVar, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
|*| Optimised : Get Display Name and Number with label and Photo Thumbnail in one query : Gets result query in less than 4 sec.
ContentResolver contntRslverVar = getContentResolver();
Cursor contctKsrVar = contntRslverVar.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.LABEL,
ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI},
null, null, null);
while (contctKsrVar.moveToNext())
{
String contctUidVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.Contacts._ID));
String contctNamVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contctMobVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int MobLblTypVar = contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int MobLblTipVar = contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
MobLblTypVar = contctKsrVar.getInt(MobLblTypVar);
String contctMobLblVar;
if(MobLblTypVar == ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM)
{
contctMobLblVar = contctKsrVar.getString(MobLblTipVar);
}
else
{
CharSequence MobLblSrgVar = ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), MobLblTypVar, "Mobile");
contctMobLblVar = MobLblSrgVar.toString();
}
String PflPicSrgVar = contctKsrVar.getString(contctKsrVar.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_THUMBNAIL_URI));
if(PflPicSrgVar != null) Uri PflPicUriVar = Uri.parse(PflPicSrgVar);
}

How to get phone numbers of contact members of a given contact group in android using cursor

I have gotten the group id from cursor. Let's say groupID as '59'. I want to get all contacts from the groups where Group_ID is 59. I have manipulated projection, selection and cursor with different classes of ContactsContract but no use. Here is what my code for getting member contacts looks like.
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection22 = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
if(groupID != null && !"".equals(groupID)) {
selection = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ " = ?";
selectionArgs = new String[] { groupID };
}
String sortOrder = null;
Cursor cursor = getContentResolver().query(uri, projection22,
selection, selectionArgs, sortOrder);
Now the cursor returns zero count. Please get me through to use the cursor in a proper way.
public void getSampleContactList(int groupID) {
contactList = new ArrayList<ConatctData>();
Uri groupURI = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID };
Cursor c = getContentResolver().query(
groupURI,
projection,
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ "=" + groupID, null, null);
while (c.moveToNext()) {
String id = c
.getString(c
.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID));
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { id }, null);
while (pCur.moveToNext()) {
ConatctData data = new ConatctData();
data.name = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
data.phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactList.add(data);
}
pCur.close();
}
}
class ConatctData {
String phone, name;
}
original post

how to read and write Android myself contact

I am working with Android contacts, which is Android 4.0.3. In the contact app, I can create, add, edit and see myself contact in it. But when I tried to get myself contact from my app using contentResolver, it did not work. How can I read and write myself contact?
Thanks in advance.
EDIT: here is my code to get contacts
List list = new ArrayList();
Uri uri = Contacts.CONTENT_URI;
String[] projection = new String[] {
Contacts._ID,
Contacts.LOOKUP_KEY,
Contacts.DISPLAY_NAME,
Contacts.HAS_PHONE_NUMBER,
Contacts.PHOTO_ID,
Contacts.LAST_TIME_CONTACTED,
Contacts.TIMES_CONTACTED
};
String sortOrder = Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
if (sortColumn != null) {
if (sortColumn.equals(Contacts.LAST_TIME_CONTACTED) || sortColumn.equals(Contacts.TIMES_CONTACTED))
sortOrder = "" + sortColumn + " COLLATE LOCALIZED DESC";
}
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, sortOrder);
while (cursor.moveToNext()){
list.add(getRecord(context, cursor));
}
return list;
} finally {
if (cursor!=null) cursor.close();
}
Try this Uri below.
Uri uri = Uri.withAppendedPath(
ContactsContract.Profile.CONTENT_URI,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
This is the path of "Myself".

Android query phonenumber to obtain rawcontactID

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.

Android get a cursor only with contacts that have an email listed

Friends I want Contacts which have email and also sort in ascending order..
any one know how to get this list and sort..
Please help me and thanks in advance.
I am using this code.
MatrixCursor matCur = new MatrixCursor(new String[] { Contacts._ID,
Contacts.DISPLAY_NAME, "photo_id", "starred" });
Cursor cEmail = WP7Main.this.managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
cEmail.moveToFirst();
if (cEmail.moveToFirst())
{
// String name =
// cursor.getString(cursor.getColumnIndexOrThrow(People.NAME));
String contactId = cEmail.getString(cEmail.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = WP7Main.this.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = " + contactId, null, null);
String emailAddress = "";
while (emails.moveToNext())
{
// This would allow you get several email addresses
if (emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)) != null)
{
String[] columnValues = {
cEmail.getString(cEmail
.getColumnIndex("_id")),
cEmail.getString(cEmail
.getColumnIndex("display_name")),
cEmail.getString(cEmail
.getColumnIndex("photo_id")),
cEmail.getString(cEmail
.getColumnIndex("starred")) };
matCur.addRow(columnValues);
}
}
emails.close();
}
Try this:
/**
* #return A managed cursor of email contacts for the given activity.
*/
public static Cursor buildFilteredEmailCursor(Activity activity) {
final String my_sort_order = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
String my_selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
String[] eproj = new String[]{
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Email.DATA};
Uri uri = android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI;
return activity.managedQuery(uri, eproj, my_selection, null, my_sort_order);
}
Use this query :
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.CONTACT_ID, Data.DISPLAY_NAME, Email.ADDRESS},
Data.MIMETYPE + "=?", new String[] {Email.CONTENT_TYPE}, Data.DISPLAY_NAME /* use Email.ADDRESS if you want to sort it using that*/);

Categories

Resources