2014-02-08 58 views
16

有很多與此相關的問題,但沒有一個能夠幫助我獲得解決方案。如何在地址簿中插入/更新/刪除聯繫人

我試圖將設備中的所有聯繫人同步到遠程服務器,並且能夠輕鬆完成,但是當更新/刪除/插入(新聯繫人)等聯繫人發生更改時無法找到解決方案。

嘗試使用ContentObserveronChange()被多次調用。很難找到聯繫人更改數據。

public class ContactService extends Service { 

    private int mContactCount; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mContactCount = getContactCount(); 

     Log.d("Contact Service", mContactCount + ""); 

     this.getContentResolver().registerContentObserver(
       ContactsContract.Contacts.CONTENT_URI, true, mObserver); 
    } 

    private int getContactCount() { 
     Cursor cursor = null; 
     try { 
      cursor = getContentResolver().query(
        ContactsContract.Contacts.CONTENT_URI, null, null, null, 
        null); 
      if (cursor != null) { 
       return cursor.getCount(); 
      } else { 
       return 0; 
      } 
     } catch (Exception ignore) { 
     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
     return 0; 
    } 

    private ContentObserver mObserver = new ContentObserver(new Handler()) { 

     @Override 
     public void onChange(boolean selfChange) { 
      super.onChange(selfChange); 

      final int currentCount = getContactCount(); 
      if (currentCount < mContactCount) { 
       // CONTACT DELETED. 

       Log.d("Contact Service", currentCount + ""); 

      } else if (currentCount == mContactCount) { 
       // CONTACT UPDATED. 
      og.d("Contact Service", currentCount+""); 

      } else { 
       // NEW CONTACT. 
       Log.d("Contact Service", currentCount + ""); 

      } 
      mContactCount = currentCount; 
     } 

    }; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     getContentResolver().unregisterContentObserver(mObserver); 
    } 
} 

但是onChange()當有更新/插入地址簿時多次調用。

任何人都可以提供給我更好的解決方案嗎?

它將不勝感激。

感謝

+0

得到了這裏的解決方案 - http://stackoverflow.com/a/31970349/1881611 – IshRoid

回答

18

事情有關onChange的是,它被調用既爲刪除/添加/更新,所以你不能僅計算聯繫人的數量,因爲人們可能會被刪除並加一那麼你有一個改變通訊錄,但數量相同。不過,查看column版本,您應該能夠評估哪個聯繫人已更新(無論您已經獲得聯繫簿的完整副本)。只需檢查版本是否大於您已有的版本(針對當前聯繫人)。

+0

有沒有辦法阻止onChange只有在有聯繫人的插入/更新/刪除時纔會被叫一次? – moDev

+0

您可以通過在特定的時間段內只接受一次呼叫來自己扼制onchange呼叫(過濾呼叫),也可以接收所有呼叫,但決定每五秒鐘將onchange發送到您自己的代碼。 – Magnus

+0

有沒有什麼辦法根據他們的狀態獲取聯繫人,如新聯繫人,更新聯繫人或刪除聯繫人? – moDev

-2

我這樣做,它爲我的呼叫日誌和聯繫人號碼更改。

in onChange 

execute a Query with contentResolver(), and put the data in a List 
then something is deleted again execute 
a Query with contentResolver(), and assign it to tempList. 
now compare the List and tempList 



@Override 
     public void onChange(boolean selfChange) { 
      super.onChange(selfChange);  
     final int List = totalContacts(); 

if (tempList < List) { 
something is deleted 
then remove the tempList from List you will get the deleted number 

}else if (tempList == List) { 
something is updated 
then remove the tempList from List you will get the deleted number 
}else { 
     something is added(reverse the Lists) 
then remove the List from tempList you will get the deleted number 
    } 
} 
+1

對不起,downvote,但這不僅是一個非常低效的查找delta的方式,也是寫得不好,格式不對僞代碼。 – mradzinski

0

按照Magnus的答案,使用此列來檢索版本代碼

ContactsContract.RawContacts.VERSION

相關問題