Android - 仅获取添加的最新联系人
Android - Get only newest contacts that were added
我知道如何从用户设备中检索联系人。
但我想知道是否有办法查询联系人 table 并只获取
添加的最新联系人?
我想做的是:
在我的应用程序中,我从设备读取联系人并将它们保存在我的应用程序的数据库中,因此用户可以select哪些是此应用程序中的收藏夹联系人。下次用户输入联系人 activity 时,我从数据库中读取数据。
现在,我想添加一个刷新按钮,这样当用户点击它时,联系人列表将更新为新的联系人。所以不用再读取整个联系人 table..我只想检索添加的最新联系人或更改的联系人(在设备中)。
有什么办法可以实现吗?
我看到的唯一与这个问题有某种关系的字段是“VERSION
”
和“DATA_VERSION
”
所以我想我还需要在我的数据库中保存每个联系人行的版本以及用户何时点击刷新我需要比较它以查看是否有任何变化..但在这种情况下我需要再次从设备中读取所有联系人...
使用ContentObserver
可以实现,但是无法获取更新了哪个联系人,只能获取本机联系人更新的通知。
getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, false, new AddressBookContentObserver());
/**
* Content observer for Address book contacts change notification.
*
* @author malik
*/
public class AddressBookContentObserver extends ContentObserver {
public AddressBookContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (DBG) {
ZKLog.d(TAG, "Contacts changes event.");
}
// We are waiting for some time, Native contact taking some time ot update, before that
// if we try to fetch the contact, its not returning the newly added contact
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// go through all the contact and check which one is missing or modified or added from your database.
}
}, 1000);
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
}
注:
We can't get which contact is added, modified or deleted
从 API 级别 18 开始,您可以使用 Contacts.CONTACT_LAST_UPDATED_TIMESTAMP
,因此可以查询最近修改(或创建)的所有联系人,并仅将那些与上次缓存进行比较联系人 ID,增量将是自上次您的代码 运行.
以来创建的联系人
我知道如何从用户设备中检索联系人。 但我想知道是否有办法查询联系人 table 并只获取 添加的最新联系人?
我想做的是:
在我的应用程序中,我从设备读取联系人并将它们保存在我的应用程序的数据库中,因此用户可以select哪些是此应用程序中的收藏夹联系人。下次用户输入联系人 activity 时,我从数据库中读取数据。
现在,我想添加一个刷新按钮,这样当用户点击它时,联系人列表将更新为新的联系人。所以不用再读取整个联系人 table..我只想检索添加的最新联系人或更改的联系人(在设备中)。
有什么办法可以实现吗?
我看到的唯一与这个问题有某种关系的字段是“VERSION
”
和“DATA_VERSION
”
所以我想我还需要在我的数据库中保存每个联系人行的版本以及用户何时点击刷新我需要比较它以查看是否有任何变化..但在这种情况下我需要再次从设备中读取所有联系人...
使用ContentObserver
可以实现,但是无法获取更新了哪个联系人,只能获取本机联系人更新的通知。
getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, false, new AddressBookContentObserver());
/**
* Content observer for Address book contacts change notification.
*
* @author malik
*/
public class AddressBookContentObserver extends ContentObserver {
public AddressBookContentObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
if (DBG) {
ZKLog.d(TAG, "Contacts changes event.");
}
// We are waiting for some time, Native contact taking some time ot update, before that
// if we try to fetch the contact, its not returning the newly added contact
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// go through all the contact and check which one is missing or modified or added from your database.
}
}, 1000);
}
@Override
public boolean deliverSelfNotifications() {
return true;
}
}
注:
We can't get which contact is added, modified or deleted
从 API 级别 18 开始,您可以使用 Contacts.CONTACT_LAST_UPDATED_TIMESTAMP
,因此可以查询最近修改(或创建)的所有联系人,并仅将那些与上次缓存进行比较联系人 ID,增量将是自上次您的代码 运行.