1.6 和 1.6 的联系 API 问题2.2

发布于 2024-09-26 00:55:09 字数 7059 浏览 5 评论 0原文

我正在寻找从设备获取联系方式(电话、电子邮件、邮政编码)的解决方案。我已经实现了 OS 1.6 和 2.2 的代码。我正在构建一个 API 级别 4 (1.6) 的应用程序,但是当我在 2.2 设备中运行构建时,我收到的电子邮件和电话字符串为空。

代码 - 适用于操作系统 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("|");
  }

代码 - 适用于操作系统 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();
   }
  }

 }

这是我找到的代码片段

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;
  }
}

有人建议我如何获取联系人信息在这段代码片段的帮助下?

谢谢 普拉蒂克·戈斯瓦米

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2024-10-03 00:55:09

该代码片段本身不会对您有太大帮助。我对此相当有信心,因为我编写了该代码片段所基于的代码。 :-)

此示例代码展示了使用条件类加载来使用 1.x 和 2.x API 获取手机上所有联系人的联系人、电子邮件和电话号码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文