歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 號碼查詢性能優化

我的需求是做一個快速撥號界面!list列表顯示所有聯系人Calllog資料!原來的做法在前面的日志中有提到!大概是先查
  1. Cursor phoneCursor = this.managedQuery(
  2. ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
  3. null, null);
 
  1. Cursor phoneCursor = this.managedQuery(  
  2.                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI, nullnull,  
  3.                 nullnull);  
再根據號碼來查聯系人
  1. if(0 < phoneCursor.getCount()){
  2. phoneCursor.moveToFirst();
  3. // find all contact list
  4. while (phoneCursor.getPosition() != phoneCursor.getCount()) {
  5. ContactEntity contactentity = new ContactEntity();
  6. contactentity.contact_id = phoneCursor
  7. .getLong(phoneCursor
  8. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
  9. contactentity.contacts_phone_type = phoneCursor
  10. .getInt(phoneCursor
  11. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
  12. contactentity.contacts_phone_number = phoneCursor
  13. .getString(phoneCursor
  14. .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(
  15. "-", "");
  16. contactCursor = this.managedQuery(
  17. ContactsContract.Contacts.CONTENT_URI, null,
  18. ContactsContract.Contacts._ID + "="
  19. + contactentity.contact_id, null, null);
  20. contactCursor.moveToFirst();
  21. contactentity.contacts_display_name = contactCursor
  22. .getString(contactCursor
  23. .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).replace(
  24. "-", "");
  25. Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,
  26. contactentity.contact_id);
  27. InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri);
  28. if(null != input){
  29. contactentity.contact_phone_bmp = BitmapFactory.decodeStream(input);
  30. }
  31. // spell name can
  32. contactentity.spellName = PinYin.getInstance(this).getPinyinString(
  33. contactentity.contacts_display_name);
  34. Log.i(TAG, "contactentity.contact_id: " + contactentity.contact_id
  35. + " contactentity.contacts_phone_type: "
  36. + contactentity.contacts_phone_type
  37. + " contactentity.contacts_phone_number: "
  38. + contactentity.contacts_phone_number
  39. + "contactentity.contacts_display_name: "
  40. + contactentity.contacts_display_name
  41. + "contactentity.contact_phone_bmp: "
  42. + contactentity.contact_phone_bmp);
  43. phoneCursor.moveToNext();
  44. customArrayList.add(contactentity);
  45. }
 
  1. if(0 < phoneCursor.getCount()){  
  2.         phoneCursor.moveToFirst();  
  3.   
  4.         // find all contact list   
  5.         while (phoneCursor.getPosition() != phoneCursor.getCount()) {  
  6.             ContactEntity contactentity = new ContactEntity();  
  7.             contactentity.contact_id = phoneCursor  
  8.                     .getLong(phoneCursor  
  9.                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));  
  10.             contactentity.contacts_phone_type = phoneCursor  
  11.                     .getInt(phoneCursor  
  12.                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));  
  13.             contactentity.contacts_phone_number = phoneCursor  
  14.                     .getString(phoneCursor  
  15.                             .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)).replace(  
  16.                                     "-""");  
  17.   
  18.             contactCursor = this.managedQuery(  
  19.                     ContactsContract.Contacts.CONTENT_URI, null,  
  20.                     ContactsContract.Contacts._ID + "="  
  21.                             + contactentity.contact_id, nullnull);  
  22.             contactCursor.moveToFirst();  
  23.             contactentity.contacts_display_name = contactCursor  
  24.                     .getString(contactCursor  
  25.                             .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)).replace(  
  26.                                     "-""");  
  27.   
  28.   
  29.             Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI,  
  30.                             contactentity.contact_id);  
  31.             InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), uri);  
  32.             if(null != input){  
  33.             contactentity.contact_phone_bmp = BitmapFactory.decodeStream(input);  
  34.             }  
  35.   
  36.             // spell name can   
  37.             contactentity.spellName = PinYin.getInstance(this).getPinyinString(  
  38.                     contactentity.contacts_display_name);  
  39.   
  40.             Log.i(TAG, "contactentity.contact_id: " + contactentity.contact_id  
  41.                     + " contactentity.contacts_phone_type: "  
  42.                     + contactentity.contacts_phone_type  
  43.                     + " contactentity.contacts_phone_number: "  
  44.                     + contactentity.contacts_phone_number  
  45.                     + "contactentity.contacts_display_name: "  
  46.                     + contactentity.contacts_display_name  
  47.                     + "contactentity.contact_phone_bmp: "  
  48.                     + contactentity.contact_phone_bmp);  
  49.             phoneCursor.moveToNext();  
  50.   
  51.             customArrayList.add(contactentity);  
  52.         }  
經過測試發現性能消耗主要在這個while循環當中!因為在循環當中在加一個查詢數據庫操作自然慢!
Copyright © Linux教程網 All Rights Reserved