我已经实现了ContentObserver,使用此方法我只能在联系人发生更改时收到通知
但我不知道哪个联系人已被添加,删除或更新?
!有什么建议 ?我怎么知道修改了哪个特定联系人?
我使用以下方法获取所有联系人列表。
public void readContacts() {
System.out.println(" reading contact ");
String phoneNumber = null;
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
StringBuffer output = new StringBuffer();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null,
null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor
.getString(cursor.getColumnIndex(_ID));
String name = cursor.getString(cursor
.getColumnIndex(DISPLAY_NAME));
// int id =
// Integer.parseInt(cursor.getString(cursor.getColumnIndex(
// HAS_PHONE_NUMBER )));
int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor
.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
output.append("\n First Name:" + name);
System.out.println(" CONTACT NAME : " + name);
Contact_Name.add(name);
// Query and loop for every phone number of the contact
Cursor phoneCursor = contentResolver.query(
PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",
new String[] { contact_id }, null);
phoneCursor.moveToNext();
phoneNumber = phoneCursor.getString(phoneCursor
.getColumnIndex(NUMBER));
output.append("\n Phone number:" + phoneNumber);
System.out.println(" CONTACT number : " + phoneNumber);
phoneNumber = phoneNumber.replaceAll("\\s", "");
// phoneNumber = phoneNumber.substring(phoneNumber.length()
// - 10);
// System.err.println("Mobile no."+phoneNumber);
Contact_Number.add(phoneNumber);
phoneCursor.close();
}
output.append("\n");
}
}
}
我观察联系人列表正在通过regestring和使用以下代码进行一些更改。
this.getContentResolver().registerContentObserver(
ContactsContract.Contacts.CONTENT_URI, true, mObserver);
private ContentObserver mObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
final int currentCount = getContactCount();
if (currentCount < mContactCount) {
System.out.println(" Deleted contact");
} else if (currentCount == mContactCount) {
System.out.println(" upated contact");
} else {
System.out.println(" inserted contact");
}
mContactCount = currentCount;
}
};
答案 0 :(得分:2)
检查版本是否大于您已经存在的版本(对于当前联系人)。这比您关注的当前流程更快,更准确,因为所有项目都会调用OnChange
(删除,添加或更新< /强>)。
因此,计算联系人数量没有多大意义,因为联系人会更新然后删除并添加新联系人。在这种情况下,计数是相同的。
http://developer.android.com/reference/android/provider/ContactsContract.SyncColumns.html#VERSION
答案 1 :(得分:0)
尝试为您使用此实现联系人数组
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ActiveList<T> extends ArrayList<T> implements Serializable {
private static final long serialVersionUID = 870605912858223939L;
transient private List<ActiveListListener<T>> listeners;
public interface ActiveListListener<T> {
void onAdd(T item);
void onInsert(int location, T item);
void onAddAll(Collection<? extends T> items);
void onClear();
}
@Override
public synchronized void clear() {
super.clear();
fireChangedEvent();
}
@Override
public synchronized int size() {
return super.size();
}
@Override
public synchronized T get(int index) {
return super.get(index);
}
@Override
public synchronized boolean add(T item) {
boolean success = super.add(item);
if (success) {
fireAddEvent(item);
}
return success;
}
@Override
public synchronized void add(int index, T item) {
super.add(index, item);
fireInsertEvent(index, item);
}
@Override
public synchronized boolean addAll(int index, Collection<? extends T> items) {
boolean success = super.addAll(index, items);
if (success) {
fireAddAllEvent(items);
}
return success;
}
@Override
public synchronized boolean addAll(Collection<? extends T> items) {
boolean success = super.addAll(items);
if (success) {
fireAddAllEvent(items);
}
return success;
}
public synchronized void addListener(ActiveListListener<T> listener) {
if (this.listeners == null) {
listeners = Lists.newArrayList();
}
this.listeners.add(listener);
}
public synchronized void removeListener(ActiveListListener<T> listener) {
if (this.listeners != null) {
this.listeners.remove(listener);
}
}
private void fireChangedEvent() {
if (this.listeners == null)
return;
for (ActiveListListener<T> listener : listeners) {
listener.onClear();
}
}
private void fireInsertEvent(int location, T item) {
if (this.listeners == null)
return;
for (ActiveListListener<T> listener : listeners) {
listener.onInsert(location, item);
}
}
private void fireAddEvent(T item) {
if (this.listeners == null)
return;
for (ActiveListListener<T> listener : listeners) {
listener.onAdd(item);
}
}
private void fireAddAllEvent(Collection<? extends T> items) {
if (this.listeners == null)
return;
for (ActiveListListener<T> listener : listeners) {
listener.onAddAll(items);
}
}
}
在您的活动或适配器
中protected ActiveList.ActiveListListener<Contact> activeListListener = new ActiveList.ActiveListListener<Contact>() {
@Override
public void onAdd(Contact c) {
// do something
}
@Override
public void onInsert(final int location, final Contact c) {
// do something
}
@Override
public void onClear() {
// do something
}
@Override
public void onAddAll(List<Contact> cs) {
// do something
}
};
并在您的数组中
((ActiveList<Contact>) this.contacts).addListener(activeListListener);