views:

52

answers:

1

Hi All,

I am looking for the solution to get contact details (Phone, Email, Zip) from the device. I have implemented code for OS 1.6 as well as for 2.2. I am building an application with API level 4 (1.6), but when I run the build in 2.2 device I am getting email and phone string null.

CODE - for OS 1.6

public void ImportCustomers(Context context) {
  StringBuilder sFirstname = new StringBuilder();
  StringBuilder sLastname = new StringBuilder();
  StringBuilder sEmail = new StringBuilder();
  StringBuilder sZip = new StringBuilder();
  StringBuilder sCell = new StringBuilder();

  String name, phoneNumber, email, emailid, contactDetails[];
  contactDetails = new String[] { People._ID, People.NAME, People.NUMBER,
    People.PRIMARY_EMAIL_ID };
  Cursor contactCursor = context.getContentResolver().query(
    People.CONTENT_URI, contactDetails, null, null, People.NAME);

  while (contactCursor.moveToNext()) {
   name = contactCursor.getString(contactCursor
     .getColumnIndex(People.NAME));
   phoneNumber = contactCursor.getString(contactCursor
     .getColumnIndex(People.NUMBER));
   emailid = contactCursor.getString(contactCursor
     .getColumnIndex(People.PRIMARY_EMAIL_ID));

   if (name.contains(" ")) {
    sFirstname.append("|" + name.split("\\ ")[0].trim());
    sLastname.append("|" + name.split("\\ ")[1].trim());
   } else if (name.contains(".")) {
    sFirstname.append("|" + name.split("\\.")[0].trim());
    sLastname.append("|" + name.split("\\.")[1].trim());
   } else if (name.contains(",")) {
    sFirstname.append("|" + name.split("\\,")[1].trim());
    sLastname.append("|" + name.split("\\,")[0].trim());
   } else {
    sFirstname.append("|" + name);
    sLastname.append("|Unknown");
   }

   if (emailid != null) {
    Cursor emailCur = context.getContentResolver().query(
      Contacts.ContactMethods.CONTENT_EMAIL_URI, null,
      People.PRIMARY_EMAIL_ID + " = ?",
      new String[] { emailid }, null);
    while (emailCur.moveToNext()) {
     email = emailCur.getString(emailCur
       .getColumnIndex(Contacts.ContactMethods.DATA));
     sEmail.append("|" + email);
     break;
    }
    emailCur.close();
    emailCur = null;
   } else
    sEmail.append("|");

   sCell.append("|" + (phoneNumber == null ? "" : phoneNumber));
   sZip.append("|");

  }
  contactCursor.close();
  contactCursor = null;

  if (sFirstname.toString().length() == 0) {
   sFirstname.append("|");
   sLastname.append("|");
   sCell.append("|");
   sEmail.append("|");
   sZip.append("|");
  }

CODE - for OS 2.2

private void populateContacts() {

  ContentResolver cr = getContentResolver();

  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
    null, null, null);

  if (cur.getCount() > 0) {

   while (cur.moveToNext()) {

    // ID AND NAME FROM CONTACTS CONTRACTS
    String id = cur.getString(cur
      .getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur
      .getString(cur
        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    Log.i("Pratik", "ID :" + id);
    Log.i("Pratik", "NAME :" + name);

    // GET PHONE NUMBERS WITH QUERY STRING
    if (Integer
      .parseInt(cur
        .getString(cur
          .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
     Cursor pCur = cr.query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
       null,
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID
         + " = ?", new String[] { id }, null);

     // WHILE WE HAVE CURSOR GET THE PHONE NUMERS
     while (pCur.moveToNext()) {
      // Do something with phones
      String phone = pCur
        .getString(pCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));

      String phoneType = pCur
        .getString(pCur
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

      Log.i("Pratik", "PHONE :" + phone);
      Log.i("Pratik", "PHONE TYPE :" + phoneType);
     }
     pCur.close();
    }

    //WHILE WE HAVE CURSOR GET THE EMAIL 
    Cursor emailCur = cr.query(
      ContactsContract.CommonDataKinds.Email.CONTENT_URI,
      null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
        + " = ?", new String[] { id }, null);
    while (emailCur.moveToNext()) {
     // This would allow you get several email addresses
     // if the email addresses were stored in an array
     String email = emailCur
       .getString(emailCur
         .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
     String emailType = emailCur
       .getString(emailCur
         .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

     Log.i("Pratik", "EMAIL :" + email);
     Log.i("Pratik", "EMAIL TYPE :" + emailType);
    }
    emailCur.close();

    //FOR GETTTING ZIP & POSTAL
    String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] addrWhereParams = new String[]{id, 
     ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; 
    Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                   null, addrWhere, addrWhereParams, null); 

    while(addrCur.moveToNext()) {
     String poBox = addrCur.getString(
                        addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
      String street = addrCur.getString(
                        addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
      String city = addrCur.getString(
                        addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
      String state = addrCur.getString(
                        addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
      String postalCode = addrCur.getString(
                        addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
      String country = addrCur.getString(
                        addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
      String type = addrCur.getString(
                        addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

      Log.i("Pratik", "POBOX :" + poBox);
     Log.i("Pratik", "STREET :" + street);
     Log.i("Pratik", "CITY :" + city);
     Log.i("Pratik", "STATE :" + state);
     Log.i("Pratik", "POSTALCODE :" + postalCode);
     Log.i("Pratik", "COUNTRY :" + country);
     Log.i("Pratik", "TYPE :" + type);
     } 
     addrCur.close();
   }
  }

 }

Here is the code snippet I have found

public void ImportCustomer(Context context) {
  int sdk = new Integer(Build.VERSION.SDK).intValue();
  if (sdk >= 5) {
   try {
    Class clazz = Class.forName("android.provider.ContactsContract$Contacts");
    CONTENT_URI = (Uri) clazz.getField("CONTENT_URI").get(clazz);
   } catch (Throwable t) {
    Log.e("PickDemo", "Exception when determining CONTENT_URI", t);
   }
  } else {
   CONTENT_URI = Contacts.People.CONTENT_URI;
  }
}

Do anyone have suggestion how can I get contacts' information with help of this code snippet?

Thanks Pratik Goswami

A: 

That code snippet itself will not help you much. I feel fairly confident about this, since I wrote the code that the snippet is based on. :-)

This sample code shows using conditional class loading to obtain contacts, emails, and phone numbers for all contacts on the phone, using the 1.x and 2.x APIs.

CommonsWare