示例#1
0
 public POJOContact(Context ctx, String phone) {
   super();
   this.phone = phone;
   Cursor contactCursor =
       ctx.getContentResolver()
           .query(
               ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
               null,
               ContactsContract.CommonDataKinds.Phone.NUMBER + "=?",
               new String[] {phone},
               null);
   contactCursor.moveToFirst();
   this.name =
       contactCursor.getString(contactCursor.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
   this.lookupKey =
       contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
   String photoId =
       contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
   if (photoId != null) {
     this.icon =
         ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(photoId));
   }
   this.phoneType =
       CommonDataKinds.Phone.getTypeLabel(
               ctx.getResources(),
               contactCursor.getInt(contactCursor.getColumnIndex(CommonDataKinds.Phone.TYPE)),
               contactCursor.getString(contactCursor.getColumnIndex(CommonDataKinds.Phone.LABEL)))
           .toString();
   contactCursor.close();
 }
  /**
   * Helper method to retrieve contact's data.
   *
   * @param contactId Contact's unique ID
   * @return Data about this contact, including all phone numbers
   */
  private String getContactData(long contactId) {
    Uri baseUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection = {CommonDataKinds.Phone.TYPE, CommonDataKinds.Phone.NUMBER};
    String selection = Data.CONTACT_ID + "=" + contactId;
    String[] selectionArgs = null;
    String orderBy = null;

    Log.d("ContentDemo", Long.toString(contactId));

    Cursor phoneCursor =
        getContentResolver().query(baseUri, projection, selection, selectionArgs, orderBy);

    int typeIdx = phoneCursor.getColumnIndex(CommonDataKinds.Phone.TYPE);
    int phoneIdx = phoneCursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);

    StringBuilder builder = new StringBuilder();
    while (phoneCursor.moveToNext()) {
      int phoneTypeResource =
          CommonDataKinds.Phone.getTypeLabelResource(phoneCursor.getInt(typeIdx));
      String phoneType = getString(phoneTypeResource);
      String phoneNumber = phoneCursor.getString(phoneIdx);
      builder.append(phoneType).append(": ").append(phoneNumber).append("\n\t");
    }
    return builder.toString();
  }
示例#3
0
  /**
   * Return a list of all contacts on this device
   *
   * @param ctx
   * @return
   */
  public static ArrayList<POJOContact> getAllContacts(Context ctx) {
    ArrayList<POJOContact> allContacts = new ArrayList<POJOContact>();

    Cursor contactCursor =
        ctx.getContentResolver()
            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    contactCursor.moveToFirst();
    while (!contactCursor.isAfterLast()) {
      POJOContact contact = new POJOContact();

      contact.lookupKey =
          contactCursor.getString(
              contactCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
      String photoId =
          contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
      if (photoId != null) {
        contact.icon =
            ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, Long.parseLong(photoId));
      }
      contact.name =
          contactCursor.getString(contactCursor.getColumnIndex(CommonDataKinds.Phone.DISPLAY_NAME));
      contact.phoneType =
          CommonDataKinds.Phone.getTypeLabel(
                  ctx.getResources(),
                  contactCursor.getInt(contactCursor.getColumnIndex(CommonDataKinds.Phone.TYPE)),
                  contactCursor.getString(
                      contactCursor.getColumnIndex(CommonDataKinds.Phone.LABEL)))
              .toString();
      contact.phone =
          contactCursor.getString(contactCursor.getColumnIndex(CommonDataKinds.Phone.NUMBER));
      allContacts.add(contact);
      contactCursor.moveToNext();
    }

    contactCursor.close();

    return allContacts;
  }