我想将安卓联系人手机克隆到我自己的SQLite db中。为了节省时间,应在Android系统中新建或更新单个联系人时触发克隆。因此,我希望有“最后修改的时间”的每一个接触。
对于API级别18或更高的用户,我似乎可以通过使用ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP.获得单个人接触的最后修改时间。然而,对于API级别17或更低的API,在此之前似乎有一些讨论建议使用"ContactsContract.RawContacts.VERSION“或"CONTACT_STATUS_TIMESTAMP”。
对于"CONTACT_STATUS_TIMESTAMP",它总是返回零或空。对于"ContactsContract.RawContacts.VERSION",当我更新一个人的联系方式的照片、电话号码或电子邮件时,版本保持不变。
很高兴有人指出我犯的错误..。
发布于 2018-03-19 16:05:46
在使用ContentObserver更改某些内容时,您可以得到通知。然后,您将需要检索正确的联系人自己。当然,这意味着当联系人发生变化或(更合理地)运行后台服务时,必须打开应用程序。
在您的服务中,创建一个内容观察者:
myObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (!selfChange) {
//Note: when this (older) callback is used you need to loop through
//and find the contact yourself (by using the dirty field)
}
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
if (!selfChange) {
//Note: if you receive a uri, it has contact id
long rawContactId = ContentUris.parseId(uri);
//Note: be careful which thread you are on here (dependent on handler)
}
}
};
//NOTE: Then you need to remember to register and unregister the observer.
//getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, myObserver);
//getContentResolver().unregisterContentObserver(myObserver);您关于使用脏的的建议并不是一个好的解决方案,因为这只是暂时表明聚合联系人(所有者)应该被更新,因为RawContact中的某些内容发生了变化。这意味着如果联系人在你的应用程序被打开之前被同步,那么脏就已经是假的(0)了。
还请注意,列的文档提到它是在API 18中添加的,因此您知道,只有在18以下才需要解决方法。因此,第一步是确保在可以的时候使用该列。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
//continue with existing code
} else {
//use workaround (start service, or at least register observer)
}https://stackoverflow.com/questions/28361228
复制相似问题