[Android] how to import android contacts in your application
A very simple way to search, select and import contact information from your phone (Android Contacts) to your android app is:
1) Send the specific android intent “ACTION_PICK”:
1 2 3 4 5 |
private void startContactPicker() { Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(contactPickerIntent, CONTACT_PICKER_REQ_CODE); } |
This intent will do for us and “for free” the operation of opening the contacts list and select the contact to import.
2) Simply manage the Intent result by reading the contact information through the resulting Content URI and access to the internal android database (SQLite):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == CONTACT_PICKER_REQ_CODE && resultCode == RESULT_OK) { Uri result = data.getData(); String id = result.getLastPathSegment(); Contact cont = new Contact(); //our internal application model of contact String whereName = ContactsContract.Data.CONTACT_ID+ " = ?"; String[] whereNameParams = new String[] { id }; Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, null); c.moveToFirst(); while (c.moveToNext()) { try { //get the mime type String mime = c.getString(c.getColumnIndex("mimetype")); //found email address if(mime.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) ) { cont.setMail(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))); } //found phone number else if(mime.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) ) { String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); int t = ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN; try { String typeStr = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); t = Integer.parseInt(typeStr); } catch(Exception ex){} switch(t) { case Phone.TYPE_COMPANY_MAIN: cont.setCompanyMainPhone(number); break; case Phone.TYPE_FAX_WORK: case Phone.TYPE_FAX_HOME: cont.setFax(number); break; case Phone.TYPE_HOME: cont.setHomePhone(number); break; case Phone.TYPE_MOBILE: default: cont.setMobilePhone(number); break; } } //found a postal address else if(mime.equals(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE) ) { cont.setAddress(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET))); cont.setPostCode(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE))); cont.setCity(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY))); cont.setCountry(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY))); } //found given and family names else if(mime.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) ) { cont.setNome(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME))); cont.setCognome(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))); } //found company else if(mime.equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) ) { cont.setRagioneSociale(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY))); } //...other???? } catch(Exception ex) { ex.printStackTrace(); } } c.close(); //... manage the new created contact } } |
Obviously it’s possible to customize the procedure in order, for example, to import all contacts or to get the list of all names and make a multiple selection before doing the contact import and so on…
Bye