How do I get a user's connection from a device's contact list?
Is it possible to get connections of any contact from the contact list of the device? Connection like google + account email id and other third party api I'm looking for a request. But the right decision did not work out. Therefore, please, friends will help me deal with this problem. thanks in advance
source to share
In a contact provider, the "connections" of a contact are just entries RawContacts
for that specific CONTACT_ID
. And their data is stored in a table Data
.
Therefore, to get all the information about all the contacts of a contact, it is enough to query the table Data
with the appropriate filter. For example:
private static void getConnections(Context context, int contactId)
{
// Read all data for contactId
String selection = RawContacts.CONTACT_ID + " = ?";
String[] selectionArgs = new String[] { String.valueOf(contactId) };
Cursor c = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);
while (c.moveToNext())
{
String accountType = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_TYPE));
String accountName = c.getString(c.getColumnIndexOrThrow(ContactsContract.RawContacts.ACCOUNT_NAME));
String dataMimeType = c.getString(c.getColumnIndexOrThrow(ContactsContract.Data.MIMETYPE));
String dataValue = c.getString(c.getColumnIndexOrThrow(ContactsContract.Data.DATA1));
Log.d("contactInfo", accountName + " (" + accountType + ") - " + dataMimeType + " - " + dataValue);
}
c.close();
}
You can filter this data using more restrictive selection
/ arguments selectionArgs
(for example, specific ones ACCOUNT_TYPE
, MIMETYPE
etc.).
Of course, you need the appropriate permission in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CONTACTS" />
source to share
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(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
source to share
This is a solution using the 3rd party Whatsapp API:
Cursor connectionCur = cr.query(ContactsContract.RawContacts.CONTENT_URI,
new String[]{ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE},
ContactsContract.RawContacts.CONTACT_ID +"=?",
new String[]{String.valueOf(id)},
null);
System.out.println("TOTAL NUMBERS OF COUNTS====================="+connectionCur.getCount());
if (connectionCur != null && connectionCur.getCount() >0)
{
while (connectionCur.moveToNext())
{
String accountName = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME));
String accountType = connectionCur.getString(connectionCur.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_TYPE));
if(accountType.contains("whatsapp"))
{
Toast.makeText(LoginScreen.this, "whatsapp == "+accountType, Toast.LENGTH_SHORT).show();
}
else if(accountType.contains("google"))
{
Toast.makeText(LoginScreen.this, "google == "+accountType, Toast.LENGTH_SHORT).show();
}
else if(accountType.contains("facebook"))
{
Toast.makeText(LoginScreen.this, "facebook == "+accountType, Toast.LENGTH_SHORT).show();
}
else if(accountType.contains("twitter"))
{
Toast.makeText(LoginScreen.this, "twitter == "+accountType, Toast.LENGTH_SHORT).show();
}
else if(accountType.contains("linkedin"))
{
Toast.makeText(LoginScreen.this, "linkedin == "+accountType, Toast.LENGTH_SHORT).show();
}
System.out.println("a========="+accountName+" b====="+accountType);
}
}
source to share