Android 联系人合约 : Last Modified Time

标签 android android-contacts last-modified

我想将 Android Contacts Phone 克隆到我自己的 SQLite 数据库中。为了节省时间,克隆应该在Android系统新建或更新单个联系人时触发。因此,我想要每个联系人的“最后修改时间”。

对于 API 级别 18 或更高版本,我似乎可以使用 ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP 获取单个联系人的最后修改时间。然而,对于 API 级别 17 或以下,似乎之前有一些讨论建议使用“ContactsContract.RawContacts.VERSION”或“CONTACT_STATUS_TIMESTAMP”。

对于“CONTACT_STATUS_TIMESTAMP”,它总是返回零或空值。对于“ContactsContract.RawContacts.VERSION”,当我更新联系人的照片、电话号码或电子邮件时,版本保持不变。

很高兴有人能指出我所犯的错误...

引用: How to get the last modification date for Contacts list (Add/Delete/Modify)

最佳答案

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

您建议使用 dirty单独不是一个好的解决方案,因为这只是暂时表明应该更新聚合联系人(所有者),因为 RawContact 中的某些内容已更改。这意味着如果联系人在您的应用程序打开之前同步,则 dirty 已经再次为 false (0)。

另请注意 the column 的文档提到它是在 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)
}

关于Android 联系人合约 : Last Modified Time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28361228/

相关文章:

android - 在 android 中存储和检索联系人

android - 如何获取在线文件的最后修改日期?

java - 如何将上次修改日期与文件上次修改日期进行比较?

powershell-3.0 - 在 PowerShell 中查找最新修改的文​​件信息

android - 无法以编程方式在android中发送带有多个附件的电子邮件

android - 过渡动画期间嵌套 fragment 消失

android - 如何在 Activity 中成功地将内容显示为来自 RunningFragment 类的 Fragment?

java - 如何在Eclipse中生成流程图?

android - 所有 Android 版本中的联系人权限

java - 试图将所有联系人放入 ListView 中?