3

NOTE: Must work on Android 1.5 - ContactsContract does not

Simple enough question. I need to know the best way to get the same list of contacts that show up when a user presses the Contacts button.

You would think something like this would work:

//For Contacts
Intent pickIntent = new Intent(Intent.ACTION_PICK, People.CONTENT_URI);
this.startActivityForResult(pickIntent, RESULT);

//For Phones
Intent pickIntent = new Intent(Intent.ACTION_PICK, Phones.CONTENT_URI);
this.startActivityForResult(pickIntent, RESULT);

The problem is that does not include secondary google accounts or Exchange contacts. By secondary accounts, in Android you can add additional gmail accounts to have the mail/contacts synced. The above intent will not list those additional contacts.

I am also told that on the HTC Desire you can add contacts to the phone that do not get synced up to Google. These contacts also do not show up.

So how do I get a real list of contacts so I can create my own list activity that works properly where the Google intent does not.

NOTE: Must work on Android 1.5 - ContactsContract does not

Cameron McBride
  • 6,779
  • 11
  • 40
  • 52

4 Answers4

3

You need to use the ContactsContract provider to read the data from database.

See this link for details on how to use ContactsContract.

HTH !

Karan
  • 12,724
  • 6
  • 40
  • 33
  • Over 30% of Android users are still on Android 1.5 and cannot use the ContactsContract class. Are there any good examples of reading out the contacts that works on ALL versions? I have to continue supporting those users. – Cameron McBride May 25 '10 at 12:17
3

See here for a detailed description of accessing contacts on Android. It also helps you creating an application that uses ContactsContract if possible but still works on older Android versions.

molnarm
  • 9,856
  • 2
  • 42
  • 60
  • Fantastic! I was already doing a reflection test on my own using the ContactsContract to see if it would work. I will implement, test on the Desire and get right back with you. – Cameron McBride May 25 '10 at 13:03
  • I coded up their example but I don't see how it helps us. You need to target the 2.0.1 or higher API in order for their example to compile. I am, therefore, assuming that your application cannot then be installed on a handset running anything less than that version and that users of earlier releases will not see your app in the Market. We need a solution that works and can be deployed on all versions. None of the examples I have seen do that, unless I am being really thick and missing something, which is quite likely. I've concluded that I have to publish two versions of my app. Not good. – Rob Kent Jun 21 '10 at 11:27
  • You can compile for a later version but still support earlier SDKs. If you use reflection you can dynamically load classes that import later APIs -- classes are not "linked" until loaded. For more on versioning see here: http://developer.android.com/guide/publishing/versioning.html#minsdkversion – sehugg Jul 15 '10 at 04:56
0

do refer the question below, How to read contacts on Android 2.0

People class is depreciated from Android 2.0 you have to use ContactsContract class instead.

Community
  • 1
  • 1
Vamsi
  • 5,853
  • 6
  • 29
  • 36
  • Over 30% of Android users are still on Android 1.5 and cannot use the ContactsContract class. Are there any good examples of reading out the contacts that works on ALL versions? I have to continue supporting those users. – Cameron McBride May 25 '10 at 12:19
  • i dont think there is any such class, you have to support them individually. – Vamsi May 26 '10 at 06:51
-1
Cursor cursor=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    while ( cursor.moveToNext() ) {
        String name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

    }
Flexo
  • 87,323
  • 22
  • 191
  • 272