How to read contacts in Android using Realm?

前端 未结 2 657
广开言路
广开言路 2020-11-27 23:54

I can\'t find any solutions to read contacts from Android and save them in Realm. Anyone done that before?

I know that I will have to use Contacts Provider

2条回答
  •  佛祖请我去吃肉
    2020-11-28 00:24

    Create RealmObject, read the data from content provider, save data to RealmObject, save data in Realm:

    // background thread
    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmContact realmContact = new RealmContact();
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
                while (phones.moveToNext()) {
                   String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                   String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   realmContact.setName(name);
                   realmContact.setPhoneNumber(phoneNumber);
                   realm.insertOrUpdate(realmContact);
                }
            }
        });
    } finally {
        if(realm != null) {
            realm.close();
        }
    }
    

    EDIT: okay, here's a trick to merging data and removing all data that's not in the list you've saving

    public class RealmContract extends RealmObject {
        @PrimaryKey
        private long id;
    
        @Index
        private String name;
    
        @Index
        private String phoneNumber;
    
        @Index
        private boolean isBeingSaved;
    
        //getters, setters
    }
    

    Then merge:

    // background thread
    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                RealmContact realmContact = new RealmContact();
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
                while (phones.moveToNext()) {
                   String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds._ID));
                   String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                   String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                   realmContact.setId(id);
                   realmContact.setName(name);
                   realmContact.setPhoneNumber(phoneNumber);
                   realmContact.setIsBeingSaved(true);
                   realm.insertOrUpdate(realmContact);
                }
                realm.where(RealmContact.class)
                     .equalTo(RealmContactFields.IS_BEING_SAVED, false) // compile 'dk.ilios:realmfieldnameshelper:1.0.0'
                     .findAll()
                     .deleteAllFromRealm(); // delete all non-saved data
                for(RealmContact realmContact : realm.where(RealmContact.class).findAll()) { // realm 0.89.0+
                    realmContact.setIsBeingSaved(false); // reset all save state
                }
            }
        });
    } finally {
        if(realm != null) {
            realm.close();
        }
    }
    

    EDIT: Refer to OP's other question for reading contact data reliably (because there's something up with the Contact LOOKUP_ID and making sure the IDs are correct): Obtaining contacts from content provider without duplicates or invalid contacts, and save to Realm

提交回复
热议问题