I want to get all contact in local address book of Android device by Group Name. Such as "Family", "Friends", "Work" ... like Address book in android device.
Here is my code:
public void getAllContactsByGroup(ContentResolver cr) {
String a = "Friends";
Uri CONTENT_URI_GR = ContactsContract.Groups.CONTENT_URI;
String GR_ID = ContactsContract.Groups._ID;
String GR_NAME = ContactsContract.Groups.TITLE;
Cursor cursor = getContentResolver().query(CONTENT_URI_GR,
null, GR_NAME + "=?", new String[] { a }, null);
while (cursor.moveToNext())
{
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
Cursor phones = cr.query(CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
countContact = phones.getCount();
while (phones.moveToNext())
{
contact_id = phones.getString(phones.getColumnIndex(_ID));
String name = phones.getString(phones.getColumnIndex(DISPLAY_NAME));
name1.add(name);
_idd.add(contact_id);
}
phones.close();
}
cursor.close();
}
I tried to get all contact in group "Friends" of Address book, but it gets all contacts of all groups.
try
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor cur = getContacts();
ListView lv = getListView();
String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this,
R.layout.main,
cur,
fields,
new int[] {R.id.txtbox});
lv.setAdapter(adapter);
}
private Cursor getContacts() {
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection =
new String[]{ ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME +
" COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
Related
I want to implement search the contacts on basis of number provided.
I have used ContactsContract to read all the contacts. I have implemented search criteria on basis of name by proving a searchView and the matching name will be displayed but I want to do the same by number also
private List<ContactItem> getContacts(String s) {
String whereString = "display_name LIKE ?";
String[] whereParams = new String[]{ "%" + s + "%"};
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, whereString, whereParams, null);
List<ContactItem> contacts = new ArrayList<>();
assert cur != null;
while (cur.moveToNext()) {
String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contacts.add(new ContactItem(name,phoneNumber));
}
cur.close();
return contacts;
}
I want to use phone number instead of display_name here. How to do that
Try the following code, should filter based on both name and number:
private List<ContactItem> getContacts(String numberOrName) {
Uri searchUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(numberOrName));
String[] projection = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = getContentResolver().query(searchUri, projection, null, null, null);
List<ContactItem> contacts = new ArrayList<>();
assert cur != null;
while (cur.moveToNext()) {
String name = cur.getString(0);
String phoneNumber = cur.getString(1);
contacts.add(new ContactItem(name, phoneNumber));
}
cur.close();
return contacts;
}
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
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
View view=View.inflate(context, R.layout.main, null);
ListView list = (ListView)view. findViewById(android.R.id.list);
String[] columns = new String[{ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.PhoneLookup.NUMBER};
int[] views = new int[] {R.id.contactName, R.id.contactID};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,R.layout.text,cur,columns, views);
list.setAdapter(adapter);
this is real example
public Cursor getAllContacts() {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.DISPLAY_NAME_ALTERNATIVE
};
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + " = '1'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder);
}
I have this method to display the contact numbers in my inbox:
public ArrayList<String> fetchInboxNumbers() {
ArrayList<String> sms = new ArrayList<String>();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms,
new String[] { "_id", "address", "date", "body" }, null, null, null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
address = cursor.getString(1); // Displays phone number
Log.d("CONTACT", address);
sms.add(address); // + " " + body
}
Log.d("NAMES IN CALLLOG", sms.get(7));
return sms;
} // END FETCHINBOX
What I'm trying to do is change String address to its contact name.
From fetchContactNumbers() and 'fetchContactNames(), how can I changeaddress` to a contact name?
Its important to understand that fetchContactNumbers and fetchContactNames() are aligned, meaning fetchContactNumbers().get(5)'s contact name is fetchContactNames().get(5)`.
In the end, fetchInboxNumbers() populates a custom adapter.
This method returns a list of all contact names:
public ArrayList<String> fetchContactNames() {
ArrayList<String> names = new ArrayList<String>();
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
names.add(name);
}
phones.close();
return names;
}
And this method lists all phone numbers:
public ArrayList<String> fetchContactNumbers() {
ArrayList<String> numbers = new ArrayList<String>();
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (phones.moveToNext()) {
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
numbers.add(phoneNumber);
}
phones.close();
return numbers;
}
You can use CONTENT_FILTER_URI to get the display name, instead of getting all names and numbers separately, something like below:
public ArrayList<String> fetchInboxNumbers() {
String displayName = "";
ArrayList<String> sms = new ArrayList<String>();
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor cursor = getContentResolver().query(uriSms,
new String[] { "_id", "address", "date", "body" }, null, null, null);
cursor.moveToFirst();
while (cursor.moveToNext()) {
address = cursor.getString(1); // Displays phone number
displayName = getDisplayName(address);
sms.add(displayName); // + " " + body
}
return sms;
} // END FETCHINBOX
private String getDisplayName(String phoneNumber) {
String displayName = "";
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = context.getContentResolver().query(uri, new String[] { Phone._ID, Phone.DISPLAY_NAME}, null, null, null);
if (cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
return displayName;
}
Is it a spelling mistake? a variable mistake?
private String getDisplayName(String phoneNyumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.
CONTENT_FILTER_URI, Uri.encode(phoneNumber));
.....
}
Your argument is having phoneNyumber and you are using phoneNumber.
I need to fetch the members for a specific group in android contacts.
I have the contact group names and their ID's
Can anyone provide me how to query the contacts provider for members in a particular group ?
Try this method:
private Cursor getContacts(String groupID) {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = 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 };
}
else
selection = "1) GROUP BY (" + ContactsContract.Data.CONTACT_ID;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC ";
return getContentResolver().query(uri, projection,
selection, selectionArgs, sortOrder);
}
This works on Android 2.3.3 and lower, but doesn't work on Android 4+ and I don't currently know why.
UPD.
Adding custom string parameter "GROUP BY" to SQL query is denied in Android 4+, so I've founded this workaround:
private Cursor getContacts(String groupID) {
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = 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 = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC ";
Cursor cursor = getContentResolver().query(uri, projection,
selection, selectionArgs, sortOrder);
MatrixCursor result = new MatrixCursor(projection);
Set<Long> seen = new HashSet<Long>();
while (cursor.moveToNext()) {
long raw = cursor.getLong(1);
if (!seen.contains(raw)) {
seen.add(raw);
result.addRow(new Object[] { cursor.getLong(0),
cursor.getLong(1), cursor.getString(2) });
}
}
return result;