在Android中获取联系人详细信息需要太长时间

时间:2014-03-12 06:56:25

标签: android android-contacts

我正在尝试从firstName获取ID,lastNamefullNamephotocontacts。我用了以下代码, 但是获取数据需要很长时间,至少需要10秒。我在线程上使用了这段代码,所以我的活动不会冻结 但是获取所有数据需要很长时间。

                String[] projection    = new String[] {ContactsContract.Contacts._ID,
                        ContactsContract.Contacts.DISPLAY_NAME_ALTERNATIVE,
                        ContactsContract.Contacts.DISPLAY_NAME,
                };



                Cursor cursor = getContentResolver().query(
                        ContactsContract.Contacts.CONTENT_URI, projection, null,
                        null, null);                    

                cursor.moveToFirst();

                if (cursor.getCount() > 0) {
                    do {
                        try {

                            contactId = cursor
                                    .getString(cursor
                                            .getColumnIndex(ContactsContract.Contacts._ID));


                            Uri contactUri = ContentUris.withAppendedId(
                                    Contacts.CONTENT_URI,
                                    Long.parseLong(contactId));
                            Uri dataUri = Uri.withAppendedPath(contactUri,
                                    Contacts.Data.CONTENT_DIRECTORY);

                            Cursor phones = getContentResolver()
                                    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                    + " = " + contactId,
                                            null, null);
                            if (phones.getCount() > 0) {
                                ContactClass info = new ContactClass();
                                info.setid(contactId);

                                try {
                                    Cursor nameCursor =  


                                            getContentResolver()
                                            .query(dataUri,
                                                    null,
                                                    Data.MIMETYPE + "=?",
                                                    new String[] { StructuredName.CONTENT_ITEM_TYPE },
                                                    null);
                                    nameCursor.moveToFirst();
                                    do {

                                        String firstName = nameCursor
                                                .getString(nameCursor
                                                        .getColumnIndex(Data.DATA2));

                                        String lastName = "";

                                        String displayname = cursor
                                                .getString(cursor
                                                        .getColumnIndex(Contacts.DISPLAY_NAME_ALTERNATIVE));
                                        if (!firstName.equals(displayname)) {
                                            lastName = nameCursor
                                                    .getString(nameCursor
                                                            .getColumnIndex(Data.DATA3));
                                        }
                                        // check lastName Value
                                        if (firstName.equals(null)
                                                && lastName.equals(null)) {
                                            info.setfirstname("unknown name");
                                        } else if (firstName.equals(null)) {
                                            info.setlastname(lastName);
                                        } else if (lastName.equals(null)) {
                                            info.setfirstname(firstName);
                                        } else {
                                            info.setfirstname(firstName);
                                            info.setlastname(lastName);
                                        }

                                    } while (nameCursor.moveToNext());
                                    nameCursor.close();
                                    info.setphoto(retrieveContactPhoto(contactId));

                                    contactinfo.add(info);

                                } catch (Exception e) {

                                }
                            }
                            phones.close();
                        }

                        catch (Exception t) {

                        }

                    } while (cursor.moveToNext());
                    cursor.close();

retrieveContactPhoto():

private String retrieveContactPhoto(String contactID) {

        Uri photoUri = ContentUris.withAppendedId(
                ContactsContract.Contacts.CONTENT_URI,
                Long.parseLong(contactID));
        final Cursor image = getContentResolver().query(photoUri,
                PHOTO_ID_PROJECTION, null, null,
                ContactsContract.Contacts._ID + " ASC");

        try {
            Integer thumbnailId = null;
            if (image.moveToFirst()) {
                thumbnailId = image.getInt(image
                        .getColumnIndex(ContactsContract.Contacts.PHOTO_ID));

                Uri uri = ContentUris.withAppendedId(
                        ContactsContract.Data.CONTENT_URI, thumbnailId);

                image.close();
                if (uri.toString().equals(
                        "content://com.android.contacts/data/0"))
                    return null;

                return uri.toString();
            }

        } finally {

            image.close();
        }
        return null;

    } 

您建议我应该做些什么来改善上述代码的性能?

1 个答案:

答案 0 :(得分:1)

我尝试使用此代码来获取联系人

try {
            String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                    + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                    + Contacts.DISPLAY_NAME + " != '' ))";

            Cursor c = cntx.getContentResolver().query(Contacts.CONTENT_URI, CONTACTS_SUMMARY_PROJECTION, select,
                    null, Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");


            for(int i=0;i<c.getCount();i++)
            {
//              contactWrap.clear();
                try {
                    contactId = 0;
                    String hasPhone = "";
                    display_name = "";
                    phoneNumber = "";

                    c.moveToPosition(i);

                    contactId =  c.getLong(0);
                    display_name = c.getString(1);
                    hasPhone = c.getString(7);

                    if (hasPhone.equalsIgnoreCase("1"))
                        hasPhone = "true";
                    else
                        hasPhone = "false" ;

                    if (Boolean.parseBoolean(hasPhone)) 
                    {
                        Cursor phones = cntx.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                        while (phones.moveToNext()) 
                        {
                            int indexPhoneType = phones.getColumnIndexOrThrow(Phone.TYPE);
                            String phoneType =  phones.getString(indexPhoneType);

                            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

                            String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));


                            contactWrap.add(new ContactsWrapper(contactId, display_name, phoneNumber,lookupKey,false,color_string));
                        }
//                      map.put(contactId, new ArrayList<ContactsWrapper>(contactWrap));
                        phones.close();
                    }
                } catch (Exception e) {

                    e.printStackTrace();
                }  
            }
        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        }

哦,你可以从这个光标或你的代码中获取contactID现在这是我的方法,它会给你联系照片,只是在其中传递联系人ID

public InputStream openPhoto(long contactId) {
     Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
     Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
     Cursor cursor = getContentResolver().query(photoUri,
          new String[] {Contacts.Photo.PHOTO}, null, null, null);
     if (cursor == null) {
         return null;
     }
     try {
         if (cursor.moveToFirst()) {
             byte[] data = cursor.getBlob(0);
             if (data != null) {
                 return new ByteArrayInputStream(data);
             }
         }
     } finally {
         cursor.close();
     }
     return null;
 }

希望这会有助于你好运的老兄