联系人显示时应用程序挂起

时间:2012-02-24 06:16:37

标签: android

我想在单个查询中获取手机联系人姓名,电话号码和电子邮件。是否可以在1个查询中获取所有内容?

我的手机中有近1000个联系人,当我尝试使用以下代码行显示我的联系时,它被绞死了:

    Cursor cursor = this.getContentResolver().query(intent.getData(), null, null, null,null); 
while (cursor.moveToNext()) 
{           
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));          
    hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    if ( hasPhone.equalsIgnoreCase("1"))
        hasPhone = "true";
    else
        hasPhone = "false" ;

    if (Boolean.parseBoolean(hasPhone)) 
    {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, 
                ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE");
        while (phones.moveToNext()) 
        {
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String name = phones.getString(phones.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));                                   
        }
        phones.close();
    }
    // Find Email Addresses
    Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null,
        ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE");// contactId+ " DESC");
    while (emails.moveToNext()) 
    {
        String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));             
        String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));               
    }
    emails.close(); 
    }  //while (cursor.moveToNext()) 

1 个答案:

答案 0 :(得分:1)

AsyncTask 中执行此操作...始终是耗时过程的首选方式...

当我有大约500个联系人时,我遇到了同样的问题..我在AsyncTask中做到了它并且运行正常..