20

I want to get contacts saved in phone only not sim contacts using new api contactContracts

i am doing this by filtering Account type (not com.anddroid.contacts.sim),this work for one handset(HTC Desire) but there is no common account type found which can work for all handsets can anybody tell me how to do that

MrSmith42
  • 9,961
  • 6
  • 38
  • 49
Naresh Kaushik
  • 302
  • 1
  • 3
  • 10
  • can't you filter by 'DeviceOnly' Account type? – systempuntoout Nov 09 '10 at 14:34
  • Take a look at the accepted answer in the following question : [How to get all android contacts but without those which are on SIM](http://stackoverflow.com/questions/4338563/how-to-get-all-android-contacts-but-without-those-which-are-on-sim) You can also look at the following answer : [Finding account nature of a contact group?](http://stackoverflow.com/questions/4614089/finding-account-nature-of-a-contact-group/4634729#4634729) I think the links above may help you in acheiving what you want. – dsr Jan 08 '11 at 16:15
  • could also try this library as per need to fetch contact [Github Project](https://github.com/nitiwari-dev/android-contact-extractor) – Nitesh Tiwari May 12 '17 at 09:58

4 Answers4

29

Give the permission in the manifest file "android.permission.READ_CONTACTS"

just copy the code and contacts will be shown in Logcat

public class GetContactsDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_getcontactsdemo);
        readContacts();
    }

    public void readContacts(){
         ContentResolver cr = getContentResolver();
         Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);

         if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    System.out.println("name : " + name + ", ID : " + id);

                    // get the phone number
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                           new String[]{id}, null);
                    while (pCur.moveToNext()) {
                          String phone = pCur.getString(
                                 pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                          System.out.println("phone" + phone);
                    }
                    pCur.close();


                    // get email and type

                   Cursor emailCur = cr.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (emailCur.moveToNext()) {
                        // This would allow you get several email addresses
                            // if the email addresses were stored in an array
                        String email = emailCur.getString(
                                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        String emailType = emailCur.getString(
                                      emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                      System.out.println("Email " + email + " Email Type : " + emailType);
                    }
                    emailCur.close();

                    // Get note.......
                    String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                    String[] noteWhereParams = new String[]{id,
                    ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
                            Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
                    if (noteCur.moveToFirst()) {
                        String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                      System.out.println("Note " + note);
                    }
                    noteCur.close();

                    //Get Postal Address....

                    String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                    String[] addrWhereParams = new String[]{id,
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
                    Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,
                                null, null, null, null);
                    while(addrCur.moveToNext()) {
                        String poBox = addrCur.getString(
                                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                        String street = addrCur.getString(
                                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                        String city = addrCur.getString(
                                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                        String state = addrCur.getString(
                                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                        String postalCode = addrCur.getString(
                                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                        String country = addrCur.getString(
                                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                        String type = addrCur.getString(
                                     addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

                        // Do something with these....

                    }
                    addrCur.close();

                    // Get Instant Messenger.........
                    String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                    String[] imWhereParams = new String[]{id,
                        ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
                    Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,
                            null, imWhere, imWhereParams, null);
                    if (imCur.moveToFirst()) {
                        String imName = imCur.getString(
                                 imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                        String imType;
                        imType = imCur.getString(
                                 imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
                    }
                    imCur.close();

                    // Get Organizations.........

                    String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
                    String[] orgWhereParams = new String[]{id,
                        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
                    Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,
                                null, orgWhere, orgWhereParams, null);
                    if (orgCur.moveToFirst()) {
                        String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
                        String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
                    }
                    orgCur.close();
                }
            }
       }
    }

}
kleopatra
  • 51,061
  • 28
  • 99
  • 211
Sumit Sharma
  • 1,847
  • 1
  • 22
  • 25
  • 1
    Don't forget to add this permission "android.permission.READ_CONTACTS" – Sumit Sharma Dec 06 '12 at 06:15
  • @Kleopatra Can you Please help me in getting sim contacts. The Above code work for Phone contacts only. – Sumit Sharma Dec 06 '12 at 14:41
  • If you have a question, please post a ... [question](http://stackoverflow.com/questions/ask) :-) Personally, I can't help you, have no experience whatever in the area. – kleopatra Dec 06 '12 at 14:47
  • 1
    i would like to know that i have implemented this thing as i want to get the all contact in my phone, but i got the some of the numbers 2 time with the different id. All information is same else the ID is different in log. How can i resolve it to get the single time? – Arpit Patel Dec 27 '12 at 08:23
  • I have 2 questions: isn't it better to use lookup-key instead of contact-id ? And, isn't it better to go over all of the phone numbers, addresses, emails etc, and only then save each into its contact (less queries and cursor creations this way)? – android developer Dec 05 '16 at 06:20
  • Thank you, works perfectly! – Philipp Cherubim Oct 08 '19 at 23:14
1

You may want to look at ContentProviders for the ContactsProvider.

The ContentProvider tutorial even uses Contacts as an example:

the URI for the table that matches phone numbers to people and the URI for the table that holds pictures of people (both controlled by the Contacts content provider) are:

android.provider.Contacts.Phones.CONTENT_URI
android.provider.Contacts.Photos.CONTENT_URI

Dmitri Plotnikov has some very useful things to say in this Google Group thread.

jamesh
  • 19,863
  • 14
  • 56
  • 96
  • 1
    thanks for reply but i need only phone contacts and android.provider.Contacts.Phones.CONTENT_URI table give me contacts of both phone and sim and also this is depricated i want to use ContactsContract.Contacts api – Naresh Kaushik Nov 10 '10 at 10:42
  • The link you've provided doesn't work anymore – android developer Dec 05 '16 at 06:21
0

You can try the following code, I got the the required details:

Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null, null,null);
while (cursor.moveToNext()) {
    listName.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
    listContactId.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)));
    if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {                
        Cursor pCur = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)),null, null);
        while (pCur.moveToNext()) {
            listMobileNo.add(pCur.getString(pCur.getColumnIndex("DATA1")));
        } 
        pCur.close();
    } else
        listMobileNo.add("");
}               
JShmay
  • 172
  • 1
  • 6
  • 18
Durai
  • 481
  • 6
  • 10
0

You must add uses_permission android:name="android.permission.READ_CONTACTS then read the contacts as below:

 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));

    }
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165