Android 号码查询性能优化

我的需求是做一个快速拨号界面!list列表显示所有联系人Calllog资料!原来的做法在前面的日志中有提到!大概是先查

Cursor phoneCursor = this.managedQuery( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

 

Cursor phoneCursor = this.managedQuery(                   ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnull,                   nullnull);  

再根据号码来查联系人

if(0 < phoneCursor.getCount()){ phoneCursor.moveToFirst(); // find all contact list while (phoneCursor.getPosition() != phoneCursor.getCount()) { ContactEntity contactentity = new ContactEntity(); contactentity.contact_id = phoneCursor .getLong(phoneCursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); contactentity.contacts_phone_type = phoneCursor .getInt(phoneCursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); contactentity.contacts_phone_number = phoneCursor .getString(phoneCursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace( "-", ""); contactCursor = this.managedQuery( ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "=" + contactentity.contact_id, null, null); contactCursor.moveToFirst(); contactentity.contacts_display_name = contactCursor .getString(contactCursor .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).replace( "-", ""); Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactentity.contact_id); InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri); if(null != input){ contactentity.contact_phone_bmp = BitmapFactory.decodeStream(input); } // spell name can contactentity.spellName = PinYin.getInstance(this).getPinyinString( contactentity.contacts_display_name); Log.i(TAG, "contactentity.contact_id: " + contactentity.contact_id + " contactentity.contacts_phone_type: " + contactentity.contacts_phone_type + " contactentity.contacts_phone_number: " + contactentity.contacts_phone_number + "contactentity.contacts_display_name: " + contactentity.contacts_display_name + "contactentity.contact_phone_bmp: " + contactentity.contact_phone_bmp); phoneCursor.moveToNext(); customArrayList.add(contactentity); }

 

if(0 < phoneCursor.getCount()){           phoneCursor.moveToFirst();              // find all contact list            while (phoneCursor.getPosition() != phoneCursor.getCount()) {               ContactEntity contactentity = new ContactEntity();               contactentity.contact_id = phoneCursor                       .getLong(phoneCursor                               .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));               contactentity.contacts_phone_type = phoneCursor                       .getInt(phoneCursor                               .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));               contactentity.contacts_phone_number = phoneCursor                       .getString(phoneCursor                               .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(                                       "-""");                  contactCursor = this.managedQuery(                       ContactsContract.Contacts.CONTENT_URI, null,                       ContactsContract.Contacts._ID + "="                               + contactentity.contact_id, nullnull);               contactCursor.moveToFirst();               contactentity.contacts_display_name = contactCursor                       .getString(contactCursor                               .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).replace(                                       "-""");                     Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,                               contactentity.contact_id);               InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri);               if(null != input){               contactentity.contact_phone_bmp = BitmapFactory.decodeStream(input);               }                  // spell name can                contactentity.spellName = PinYin.getInstance(this).getPinyinString(                       contactentity.contacts_display_name);                  Log.i(TAG, "contactentity.contact_id: " + contactentity.contact_id                       + " contactentity.contacts_phone_type: "                       + contactentity.contacts_phone_type                       + " contactentity.contacts_phone_number: "                       + contactentity.contacts_phone_number                       + "contactentity.contacts_display_name: "                       + contactentity.contacts_display_name                       + "contactentity.contact_phone_bmp: "                       + contactentity.contact_phone_bmp);               phoneCursor.moveToNext();                  customArrayList.add(contactentity);           }  

经过测试发现性能消耗主要在这个while循环当中!因为在循环当中在加一个查询数据库操作自然慢!

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wywxxd.html