how to get whats app contacts' phone numbers - android

Hi I am currently trying to implement WhatsApp with an application I am working on. I want to be able to display all the WhatsApp contacts that a user has along with their phone number. So far I have been able to grab the name of their WhatsApp contact but I have not been able to grab the phone number of their WhatsApp contact, here is the code that I am using to get the list of a users WhatsApp contacts.
cursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, new String[] {
ContactsContract.RawContacts.CONTACT_ID, ContactsContract.RawContacts.DISPLAY_NAME_PRIMARY
}, ContactsContract.RawContacts.ACCOUNT_TYPE + "= ?", new String[] {
"com.whatsapp"
}, null);
int contactNameColumn = cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID);
while (cursor.moveToNext()) {
String name = cursor.getString(contactNameColumn);
System.out.println(name);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] {
Integer.toString(contactNameColumn)
}, null);
while (pCur.moveToNext()) {
String phonenumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("PHONE: " + phonenumber);
}
pCur.close();
}
cursor.close();
When I print out the name it prints it out correctly, I then use that id to query the data table in order to get their phone number, but that always returns null, can anyone help me out please.
Thanks

Use following code to get WhatsApp contacts from Phonebook with associated whatsApp number :
private void displayWhatsAppContacts() {
final String[] projection = {
ContactsContract.Data.CONTACT_ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data.MIMETYPE,
"account_type",
ContactsContract.Data.DATA3,
};
final String selection = ContactsContract.Data.MIMETYPE + " =? and account_type=?";
final String[] selectionArgs = {
"vnd.android.cursor.item/vnd.com.whatsapp.profile",
"com.whatsapp"
};
ContentResolver cr = getContentResolver();
Cursor c = cr.query(
ContactsContract.Data.CONTENT_URI,
projection,
selection,
selectionArgs,
null);
while (c.moveToNext()) {
String id = c.getString(c.getColumnIndex(ContactsContract.Data.CONTACT_ID));
String number = c.getString(c.getColumnIndex(ContactsContract.Data.DATA3));
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.v("WhatsApp", "name " +name + " - number - "+number);
}
Log.v("WhatsApp", "Total WhatsApp Contacts: " + c.getCount());
c.close();
}

Change your pCur like this
if (name != null) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{name}, null);
//Iterate cursor here...
}
And it is better to use proper naming conventions for variables. Why because here the varible name represents the id of the conatact.

Related

Unable to identify WhatsApp contacts

I'm trying to write an Android program which can identify if a given contact number is associated with WhatsApp or not. I managed to find out if a particular contact name has WhatsApp account or not. How can I find out which contact corresponding to that contact name has WhatsApp? Currently I'm using the following code:
public void identifyWhatsappContact() {
Cursor c = ctx.getContentResolver().query(
RawContacts.CONTENT_URI,
new String[] { RawContacts.CONTACT_ID, RawContacts.DISPLAY_NAME_PRIMARY },
RawContacts.ACCOUNT_TYPE + "= ? AND " + StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
new String[] { "com.whatsapp", "John Doe" },
null);
ArrayList<String> myWhatsappContacts = new ArrayList<String>();
int contactNameColumn = c.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY);
while (c.moveToNext())
{
myWhatsappContacts.add(c.getString(contactNameColumn));
Log.v("WHATSAPP", c.getString(contactNameColumn)+"");
}
}
I'm able to identify if "John Doe" has WhatsApp or not, but I can't figure out which phone number of "John Doe" has WhatsApp (assuming the contact has multiple phone numbers). Can anybody please help me? I've already tried the solution here, but it's not working for me.
This example extract all the WhatsApp numbers of a contact name:
public void getWhatsAppNumbers(String contactName) {
Cursor cursor1 = getContentResolver().query(
ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts._ID},
ContactsContract.RawContacts.ACCOUNT_TYPE + "= ? AND " + ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME_PRIMARY + " = ?",
new String[]{"com.whatsapp", contactName},
null);
while (cursor1.moveToNext()) {
String rawContactId = cursor1.getString(cursor1.getColumnIndex(ContactsContract.RawContacts._ID));
Cursor cursor2 = getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
new String[]{ContactsContract.Data.DATA3},
ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.Data.RAW_CONTACT_ID + " = ? ",
new String[]{"vnd.android.cursor.item/vnd.com.whatsapp.profile", rawContactId},
null);
while (cursor2.moveToNext()) {
String phoneNumber = cursor2.getString(0);
if (TextUtils.isEmpty(phoneNumber))
continue;
if (phoneNumber.startsWith("Message "))
phoneNumber = phoneNumber.replace("Message ", "");
Log.d("whatsapp", String.format("%s - %s", contactName, phoneNumber));
}
}
}

context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);

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.

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 Applicaiton - How to get birthday of a contact

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();

Android: adding a birthday event to an android contact programmatically

I am trying to birthday information to a contact. I use lookupkey to identify my contacts (as it is safer than just relying on contactId). In order to be able to insert the Event into the database i need a raw_contact_id ... so i'm trying to get this id:
String where = ContactsContract.Data.LOOKUP_KEY + " = ? AND "
+ ContactsContract.Data.MIMETYPE + " = ?";
String[] params = new String[] { lookupKey,
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };
Cursor cursor = contentResolver.query(
ContactsContract.Data.CONTENT_URI, null, where, params, null);
if (cursor.moveToFirst()) {
birthdayRow = cursor.getInt(idIdx);
long rawContactId = cursor.getLong(cursor
.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));
}
The problem is that if there is not birthday event set for a contact then this cursor i receive is empty ... and i don't know how to insert this event without a raw_contact_id. In order to insert the event i do the folowing:
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE, Event.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE,
birthdayStartDate);
values.put(ContactsContract.CommonDataKinds.Event.TYPE,
ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE,
context.getString(R.string.birthday_label));
if (birthdayRow >= 0) {
int result = contentResolver.update(
ContactsContract.Data.CONTENT_URI, values,
ContactsContract.Data._ID + " = " + birthdayRow, null);
Log.i("ContactList", "update result: " + result);
} else {
Uri result = contentResolver.insert(
ContactsContract.Data.CONTENT_URI, values);
Log.i("ContactList", "update result: " + result);
}
So please advice what shall i do, is there any way to add this event to the ContactData whitout a raw_contact id? Also i find strange the fact that for other ContactData like nickname i am doing the same thing and i dont get an empty cursor for the params
String[] params = new String[] { String.valueOf(lookupKey),
ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE };
even if the contact has no nickname.
Use this to get the raw contact id before performing the insert.
long rawContactId = -1;
String[] projection = new String[]{ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID};
String selection = ContactsContract.CommonDataKinds.Event.CONTACT_ID + "=?";
String[] selectionArgs = new String[]{
String.valueOf(bdayContact.getId()) };
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, selection, selectionArgs, null);
try {
if (c.moveToFirst()) {
rawContactId = c.getLong(0);
}
} finally {
c.close();
}

Categories

Resources