2011-08-23 39 views
6

Có cách nào nhận tất cả các số điện thoại cho tất cả các số liên lạc mà không thực hiện truy vấn riêng cho từng số liên lạc không? (sử dụng Android 2.0 trở lên). Nó thực sự chậm nếu bạn có hơn 100 địa chỉ liên lạc (không thể sử dụng trên điện thoại cấp thấp), tôi tự hỏi liệu tôi có thể thực hiện truy vấn hiệu quả hơn hay không.Cách hiệu quả để tải tất cả các số liên lạc và tất cả các số điện thoại (Android 2.0)

Hiện tại tôi đang nhận được một con trỏ với tất cả địa chỉ liên hệ ContactsContract.Contacts.IN_VISIBLE_GROUP hợp lệ rồi truy vấn riêng biệt cho từng liên hệ để nhận tất cả số của họ.

Snippet từ get hệ chỉ geting tên và tra cứu chính:

Uri uri = ContactsContract.Contacts.CONTENT_URI; 

String[] projection = new String[] { 
    ContactsContract.Contacts.DISPLAY_NAME, 
    ContactsContract.Contacts.LOOKUP_KEY}; 

Sau đó, mỗi lần tiếp xúc sử dụng chìa khóa tra cứu.

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey); 
Uri res = ContactsContract.Contacts.lookupContact(contentResolver, lookupUri); 

String[] projection = new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER}; 

...

Cursor phones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, phoneProjection, selection, selectionArgs, sortOrder); 
+0

rằng các liên kết cho câu hỏi này? – scottyab

+0

Bạn phải gửi biểu mẫu hai lần. Điểm tốt. Đã bỏ phiếu để đóng. – scottyab

Trả lời

15

Kiểm tra xem mã dưới đây sẽ giúp

public ArrayList<PhoneContactInfo> getAllPhoneContacts() { 
    Log.d("START","Getting all Contacts"); 
    ArrayList<PhoneContactInfo> arrContacts = new ArrayList<PhoneContactInfo>(); 
    PhoneContactInfo phoneContactInfo=null;  
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    Cursor cursor = context.getContentResolver().query(uri, new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone._ID}, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"); 
    cursor.moveToFirst(); 
    while (cursor.isAfterLast() == false) 
    { 
     String contactNumber= cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
     String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     int phoneContactID = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)); 


     phoneContactInfo = new PhoneContactInfo(); 
     phoneContactInfo.setPhoneContactID(phoneContactID);    
     phoneContactInfo.setContactName(contactName);     
     phoneContactInfo.setContactNumber(contactNumber); 
     if (phoneContactInfo != null) 
     { 
      arrContacts.add(phoneContactInfo); 
     } 
     phoneContactInfo = null; 
     cursor.moveToNext(); 
    }  
    cursor.close(); 
    cursor = null; 
    Log.d("END","Got all Contacts"); 
    return arrContacts; 
} 
+1

đẹp nhất, giảm thời gian tải từ 8,7 xuống 2,4 giây :) – scottyab

+0

sau khi chèn tiếp xúc mới vào nguồn gốc làm cách nào tôi có thể nhận id liên hệ gốc – AndroidRaji

0

Tại sao bạn d sử dụng không?

Contract.Contacts.IN_VISIBLE_GROUP

Có phải tất cả địa chỉ liên lạc trong android giao cho Contract.Contacts.IN_VISIBLE_GROUP

Các vấn đề liên quan