I'm trying to get a random contact from a phone's contact list, but without noticeably slowing down the phone. That means that I can't just grab all the contacts and stick them into an array and pick a random one from that array. I'd like to be able to get a random contact without having to get all of the contacts first. Is this possible, and if so, how would I go about doing it?
            Asked
            
        
        
            Active
            
        
            Viewed 1,368 times
        
    1 Answers
3
            Updated to use non-deprecated code. Query based on this answer: How to read contacts on Android 2.0
Cursor managedCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
Then it's just a matter of getting the size of the Cursor:
int size = managedCursor.getCount();
get a random one, read it and check if it has phone numbers. If not, choose another one:
boolean found = false;
Random rnd = new Random();
while(!found) {
  int index = rnd.nextInt(size);    
  managedCursor.moveToPosition(index);
  String name = managedCursor.getString(people.getColumnIndex(PhoneLookup.DISPLAY_NAME));
  found = Boolean.parseBoolean(managedCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))); 
  if (found) {
    Cursor phones = getContentResolver().query( 
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
    while (phones.moveToNext()) { 
     String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
     Log.d("Phone found:", phoneNumber);                 
    } 
    phones.close();
  }
}
I don't see how you could pick a random one otherwise. And this should not slow the phone down unless it's a really large contacts list.
Now it checks for the presence of phone numbers and reads all of them if found. If not, it chooses another entry.
- 
                    That would probably work, but the Contact.People is depreciated, and I'd rather not use depreciated code. – Elec0 Apr 22 '11 at 21:26
- 
                    @Elec0 Very true. See updated response. The main part (choosing a random contact without actually reading all of them) is still valid. – Aleadam Apr 22 '11 at 21:45
- 
                    OK. Let me know if that approach slows down your app or not. – Aleadam Apr 22 '11 at 23:23
- 
                    It doesn't, but now I've got another question. I need to make sure the contact has a phone number, as I'm trying to send a text message. I can't figure out how that would work. – Elec0 Apr 22 '11 at 23:37
- 
                    This should set you up. Make sure you go over the link I posted also, it is a very comprehensive answer. Good luck. – Aleadam Apr 23 '11 at 00:01
- 
                    Is this fast enough to use on the main thread, for example in the onCreate of an Activity? I'm worried about ANRs. (Or should I use a CursorLoader to to perform the cursor query on a background thread so that it does not block the application's UI?) – TechAurelian Mar 12 '14 at 19:34
- 
                    @AnAurelian Its not that heavy but any of your Cursor operation should be invoked in non-ui thread – Udi Oshi Mar 03 '18 at 20:31
 
     
    