如何使用Realm阅读Android中的联系人?

我找不到任何解决方案来从Android读取联系人并将其保存在Realm中.以前有人这样做过吗?

我知道我将不得不使用Contacts Provider,但这就是我所知道的. AFAIK,Realm不支持Cursor所以…还有什么?

编辑:

realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            Contact realmContact = new Contact();
            String filter = "" + ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and "
                    + ContactsContract.CommonDataKinds.Phone.TYPE +"="
                    + ContactsContract.CommonDataKinds.Phone.TYPE_MAIN;

            Cursor phones = getActivity()
                    .getContentResolver()
                    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, filter, null, null);

            while (phones.moveToNext()) {
                String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._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.setNumber(phoneNumber);
                realmContact.setIsBeingSaved(true);
                realm.insertOrUpdate(realmContact);
            }

            /** merge mechanism */
            realm.where(Contact.class)
                    .equalTo("isBeingSaved", false)
                    .findAll()
                    .deleteAllFromRealm(); // delete all non-saved data
            for(Contact contact : realm.where(Contact.class).findAll()) {
                realmContact.setIsBeingSaved(false); // reset all save state
            }

Contact.class

public class Contact extends RealmObject{

@PrimaryKey
private String id;

@Index
private String name;

@Index
private String number;

@Index
private boolean isBeingSaved;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

public boolean getIsBeingSaved() {
    return isBeingSaved;
}

public void setIsBeingSaved(boolean beingSaved) {
    isBeingSaved = beingSaved;
}

}

解决方法:

创建RealmObject,从内容提供者读取数据,将数据保存到RealmObject,在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();
    }
}

编辑:好的,这是一个合并数据和删除所有不在列表中的数据的技巧

public class RealmContract extends RealmObject {
    @PrimaryKey
    private long id;

    @Index
    private String name;

    @Index
    private String phoneNumber;

    @Index
    private boolean isBeingSaved;

    //getters, setters
}

然后合并:

// 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();
    }
}

编辑:请参阅OP的其他问题,以便可靠地读取联系人数据(因为有一些联系人LOOKUP_ID,并确保ID正确):Obtaining contacts from content provider without duplicates or invalid contacts, and save to Realm

上一篇:从Android中的另一个线程进行Retrofit和Realm访问


下一篇:java – 如何在领域浏览器中查看我的Realm文件?