如何获得一个请求所有联系人信息包括姓名,地址和组织名称?

时间:2014-12-08 09:11:29

标签: android

如何从一个请求中获取所有信息 - 姓名,地址和组织名称? 我需要在一个请求中为来自联系人的每个联系人获取此字段:

名称,街道,城市,地区,邮政编码,国家,类型(地址类型),组织名称

我使用这段代码,但速度很慢(可以优化吗?):

public static void readContacts(Context context) {
  long startTime = System.currentTimeMillis();
  ContentResolver cr = context.getContentResolver();

  String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);

  if (cur.getCount() > 0) {
     while (cur.moveToNext()) {
        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
        // Log.v("name : " + name + ", ID : " + id);

        // Get Postal Address....

        String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
        String[] addrWhereParams = new String[] { id, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE };
        String[] tableColumns = new String[] { ContactsContract.CommonDataKinds.StructuredPostal.STREET, ContactsContract.CommonDataKinds.StructuredPostal.CITY,
              ContactsContract.CommonDataKinds.StructuredPostal.REGION, ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
              ContactsContract.CommonDataKinds.StructuredPostal.TYPE };
        Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, tableColumns, addrWhere, addrWhereParams, null);
        while (addrCur.moveToNext()) {
           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));
           int intType = 0;
           try {
              intType = Integer.valueOf(type);
           } catch (Exception e) {
              Log.e(e);
           }

           String orgName = "";
           if (intType != Address.TYPE_OTHER) {
              orgName = getOrganization(cr, id);
              Log.v("id=" + id + " Organization=" + orgName);
           }

           Address address = new Address(id, name, orgName, street, city, state, postalCode, country, intType);
           if (filterType == Address.TYPE_ALL) {
              addresses.add(address);
           } else if (filterType == Address.TYPE_ALL_WITH_ADDRESS && intType != Address.TYPE_OTHER) {
              addresses.add(address);
           } else if (filterType == Address.TYPE_HOME && intType != Address.TYPE_HOME) {
              addresses.add(address);
           } else if (filterType == Address.TYPE_WORK && intType != Address.TYPE_WORK) {
              addresses.add(address);
           }

        }
        addrCur.close();
     }
  }
  Log.v((System.currentTimeMillis() - startTime) / Const.SEC + " sec");
}

0 个答案:

没有答案