0

我已經實現了一個ContentObserver,使用這種方法,我只能夠被通知如果一個聯繫人發生變化Android如何檢測聯繫人列表已更改?

但我不知道哪個聯繫人被添加,刪除或更新?

!有什麼建議麼 ?我如何知道哪個特定聯繫人被修改?

我通過使用以下方法獲取所有聯繫人列表。

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

爲什麼'mObserver'的'registerContentObserver'後的聲明? – Manu 2014-09-05 12:41:29

+0

@Manu我已經在我的代碼中完美地聲明瞭這一點。這裏只有我在registerContentObserver – 2014-09-05 12:43:15

回答

2

檢查版本是否大於您已有的版本(針對當前聯繫人)。由於OnChange被稱爲所有物品(刪除,更新),這是快速和更準確的,然後您當前正在跟蹤的過程。

因此,統計聯繫人數並沒有多大意義,因爲聯繫人已被更新,然後被刪除並添加了新聯繫人。在這種情況下計數是相同的。

http://developer.android.com/reference/android/provider/ContactsContract.SyncColumns.html#VERSION

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

見例如https://github.com/davidevallicella/UnivrApp/

+0

之後鍵入它是一段冗長的代碼。如何使用聯繫人的版本http://developer.android.com/reference/android/provider/ContactsContract.SyncColumns – min2bro 2014-09-05 13:10:31

相關問題