Hello. How can I get a list of contacts in the Android OS? Suppose you need to get a list of contacts in the form of pairs <Номер_Телефона; Имя_Контакта> <Номер_Телефона; Имя_Контакта> . Thank you in advance.
- I, having read everything, did not understand how to get the list of contacts. Please give an example of the finished code. - Rer 2:28 pm
- In Cursor contacts - all contacts, it remains to pick it and pull the necessary fields. - Gorets pm
- Well it is clear. And an example ??? - Rer
- An example of how to pull data from a cursor? This is not serious ...)) I have at hand, but there is a lot of excess, if no one throws, tomorrow I will clean and throw. - Gorets pm
- A lot of excess ???? Maybe I can figure it out! - Rer
|
4 answers
First, you need to add READ_CONTACTS permissions to the manifest.
<activity android:permission="android.permission.READ_CONTACTS" ... /> Secondly, you need to request a list (simplified):
Uri uri = ContactsContract.Contacts.CONTENT_URI; Cursor contacts = getContentResolver().query(uri, null, null, null, null); - Murmur, thanks for the reply. Well, I got the name of the contact. But it seems that the phone number is not stored in the "contacts" table. Quite cleverly arranged database scheme for Android. No one will tell you how to reach the phone number of this contact? Suppose I now know his _id from the table "contacts". - zugzug
- oneIt seems to have figured it out himself. With this: habrahabr.ru/blogs/android_development/130148/#habracut stackoverflow.com/questions/8868487/… - zugzug
- The link indicated at the very beginning of the answer is not working - Chekist
|
My example of implementation, works very fast and does not do a bunch of requests for the phones for each specific contact
private static final String CONTACT_ID = ContactsContract.Contacts._ID; private static final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; private static final String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; private static final String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; private static final String PHONE_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; public static ArrayList<Contact> getAll(Context context) { ContentResolver cr = context.getContentResolver(); Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[]{PHONE_NUMBER, PHONE_CONTACT_ID}, null, null, null ); if(pCur != null){ if(pCur.getCount() > 0) { HashMap<Integer, ArrayList<String>> phones = new HashMap<>(); while (pCur.moveToNext()) { Integer contactId = pCur.getInt(pCur.getColumnIndex(PHONE_CONTACT_ID)); ArrayList<String> curPhones = new ArrayList<>(); if (phones.containsKey(contactId)) { curPhones = phones.get(contactId); } curPhones.add(pCur.getString(pCur.getColumnIndex(PHONE_CONTACT_ID))); phones.put(contactId, curPhones); } Cursor cur = cr.query( ContactsContract.Contacts.CONTENT_URI, new String[]{CONTACT_ID, DISPLAY_NAME, HAS_PHONE_NUMBER}, HAS_PHONE_NUMBER + " > 0", null, DISPLAY_NAME + " ASC"); if (cur != null) { if (cur.getCount() > 0) { ArrayList<Contact> contacts = new ArrayList<>(); while (cur.moveToNext()) { int id = cur.getInt(cur.getColumnIndex(CONTACT_ID)); if(phones.containsKey(id)) { Contact con = new Contact(); con.setMyId(id); con.setName(cur.getString(cur.getColumnIndex(DISPLAY_NAME))); con.setPhone(TextUtils.join(",", phones.get(id).toArray())); contacts.add(con); } } return contacts; } cur.close(); } } pCur.close(); } return null; } The simplest implementation of the class Contact
public class Contact{ private String _name = ""; private String _phone = ""; private int _my_id; public void setName(String name) { _name = name; } public void setPhone(String phone) { _phone = phone; } public void setMyId(int my_id) { _my_id = my_id; } } |
Nikita Leshchev, thanks for your implementation - she really helped. True, instead of the phone number, the id was displayed, so I had to modify it a bit. Here is a modified version:
Instead
while (pCur.moveToNext()) { Integer contactId = pCur.getInt(pCur.getColumnIndex(PHONE_CONTACT_ID)); ArrayList<String> curPhones = new ArrayList<>(); if (phones.containsKey(contactId)) { curPhones = phones.get(contactId); } curPhones.add(pCur.getString(pCur.getColumnIndex(PHONE_CONTACT_ID))); phones.put(contactId, curPhones); } used
while (pCur.moveToNext()) { Integer contactId = pCur.getInt(pCur.getColumnIndex(PHONE_CONTACT_ID)); ArrayList<String> curPhones = new ArrayList<>(); if (phones.containsKey(contactId)) { curPhones = phones.get(contactId); } curPhones.add(pCur.getString(0)); phones.put(contactId, curPhones); } |
something like this
@Override public ArrayList<Contact> getAll(Context context) { ArrayList<Contact> contacts=new ArrayList<Contact>(); Contact contact; Cursor cursor=context.getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null ); if(cursor.getCount() > 0) { while(cursor.moveToNext()) { contact=new Contact(); final String id=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); contact.setId(id); String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); contact.setContactName(name); if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Log.i(TAG, "Contact name=" + name + ", Id=" + id); // get the phone number Cursor pCur=context.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); while(pCur.moveToNext()) { String phone=pCur.getString( pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String label= HelpUtils.getPhoneLabel(context, pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)), pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL))); PhoneNumber phoneNumber=new PhoneNumber(); phoneNumber.setPhoneNumber(phone); phoneNumber.setType(label); contact.addPhoneNumber(phoneNumber); Log.i(TAG, "phone=" + phone); } pCur.close(); contacts.add(contact); } } } return contacts; } |