How to get all contacts from phonebook & SIM card in Android?

前端 未结 3 1813
太阳男子
太阳男子 2021-02-03 16:15

I am working to get all the contacts from phone book and SIM card in my application. I want to store those all contacts in my application SQLite DB. The code with which I am wor

3条回答
  •  故里飘歌
    2021-02-03 16:52

    The following code will return all the names of the contact

      public static Cursor get(Context c){
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME };
        String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER
                + " = '1'";
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";
    
        Cursor cursor = c.getContentResolver().query(uri, projection,
                selection, null, sortOrder);
        return cursor;
    }
    

    To get all the data you can do the following

        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.CommonDataKinds.Phone._ID,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.Phone.STARRED,
                ContactsContract.CommonDataKinds.Phone.TYPE };
        String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                + " COLLATE LOCALIZED ASC";
    
        Cursor cursor = context.getContentResolver().query(uri, projection,
                null, null, sortOrder);
    

    From the cursor you can get all the data

提交回复
热议问题