【问题标题】:How can get data from address-book faster in Android?如何在 Android 中更快地从通讯录中获取数据?
【发布时间】:2014-02-27 04:19:16
【问题描述】:

我创建了一个应用程序来获取 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 应用程序)或提高更新速度、重新加载页面?

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

【问题讨论】:

    标签: android asynchronous contacts updates addressbook


    【解决方案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();
        }
    

    【讨论】:

      猜你喜欢
      • 2015-10-23
      • 1970-01-01
      • 2012-11-14
      • 2018-04-16
      • 2013-04-03
      • 1970-01-01
      • 2018-10-21
      • 2021-04-28
      • 1970-01-01
      相关资源
      最近更新 更多