如何在Android中更快地从地址簿中获取数据?

时间:2014-02-27 04:19:16

标签: android asynchronous contacts updates addressbook

我创建了一个应用程序来获取Android设备的地址簿(电话簿)中的所有联系人,例如Viper应用程序。

这是我的代码:

private void Import_contacts_from_address_book() {
        // TODO Auto-generated method stub
        String phoneNumber = null;
        String email = null;
        Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
        String _ID = ContactsContract.Contacts._ID;
        String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
        String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;

        Uri PHONECONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String PHONECONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
        String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

        Uri EMAILCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
        String EMAILCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
        String EMAIL = ContactsContract.CommonDataKinds.Email.DATA;

        StringBuffer output = new StringBuffer();
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
        System.out.println("---------------------->"+cursor.getCount());
        if(cursor.getCount() >0){
            countContact = cursor.getCount();
            while(cursor.moveToNext()){
                String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
                String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));


                //------------------- Catch phone number, neu phone >0 thi get phone, ko thi lam chuyen khac
                //if(hasPhoneNumber > 0 ){
                    output.append("\nFirst Name: "+name);
                    // Query and loop for every phone number of the contact
                    Cursor phoneCursor = contentResolver.query(PHONECONTENT_URI, null, PHONECONTACT_ID + "=?", new String[]{contact_id}, null);
                    while(phoneCursor.moveToNext()){
                        phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                        output.append("\n Phone number: "+phoneNumber);
                    }
                    phoneCursor.close();
                    // Query and loop for every email of the contact

                    Cursor emailCurosr = contentResolver.query(EMAILCONTENT_URI, null, EMAILCONTACT_ID+"=?",new String[]{contact_id},null);
                    while(emailCurosr.moveToNext()){
                        email = emailCurosr.getString(emailCurosr.getColumnIndex(EMAIL));
                        output.append("\nEmail: "+email);
                    }
                    emailCurosr.close();
                //}
                output.append("\n");
            }
            cursor.close();
        }
        txtViewContactsInfor.setText("Contacts: "+String.valueOf(countContact));
        outputText.setText(output.toString());
    }

我尝试导入1000个联系人,至少需要45秒。

  • 有没有办法提高速度?

  • 当我在Android设备中更改地址簿(电话簿)联系人的某些信息时。我使用onResume()更新我的应用程序的新信息,但它重新加载所有页面所以需要很长时间。但我在Viper上看到,数据立即更新。那么,有没有我可以将数据从我的设备更新或同步到我的应用程序,如Viper应用程序或提高速度更新,重新加载页面?

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

在android上查询操作是非常长的操作,因为每次调用查询时,系统都会打开数据库文件。在android中打开文件很昂贵。您应该尽可能少地调用查询。这是我的解决方案:

    public class Contact {
        public String name;
        public List<String> phones;
        public List<String> emails;
    }

    private static final Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    private static final String _ID = ContactsContract.Contacts._ID;
    private static final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;

    private static final Uri PHONE_CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    private static final String PHONE_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
    private static final String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;

    private static final Uri EMAIL_CONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    private static final String EMAIL_CONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID;
    private static final String EMAIL = ContactsContract.CommonDataKinds.Email.DATA;


    private Collection<Contact> importContactsFromAddressBook() {
        Map<String, Contact> contactMap = new HashMap<String, Contact>();
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                String contactId = cursor.getString(cursor.getColumnIndex(_ID));
                String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
                Contact contact = new Contact();
                contact.name = name;
                contact.phones = new ArrayList<String>();
                contact.emails = new ArrayList<String>();
                contactMap.put(contactId, contact);
            }
        }
        cursor.close();

        // Query and loop for every phone number of the contact
        Cursor phoneCursor = contentResolver.query(PHONE_CONTENT_URI, null, null, null, null);
        while (phoneCursor.moveToNext()) {
            String contactId = phoneCursor.getString(phoneCursor.getColumnIndex(PHONE_CONTACT_ID));
            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
            Contact contact = contactMap.get(contactId);
            contact.phones.add(phoneNumber);
        }
        phoneCursor.close();

        // Query and loop for every email of the contact

        Cursor emailCursor = contentResolver.query(EMAIL_CONTENT_URI, null, EMAIL_CONTACT_ID + "=?", new String[]{contactId}, null);
        while (emailCursor.moveToNext()) {
            String contactId = emailCursor.getString(emailCursor.getColumnIndex(EMAIL_CONTACT_ID));
            String email = emailCursor.getString(emailCursor.getColumnIndex(EMAIL));
            Contact contact = contactMap.get(contactId);
            contact.emails.add(email);
        }
        emailCursor.close();
        return contactMap.values();
    }