我正在使用Outlook Express創建藍牙同步應用程序。所有的工作都做得非常好,但我還有一個小問題。當我將我的聯繫人從Outlook同步到Android時,它會合並具有相似名稱的聯繫人。例如,如果我有兩個名爲「Najhi」和「Najhi Ullah」的聯繫人,那麼在同步後,他們將以Android名稱「Najhi」合併。有沒有解決方案以編程方式分離所有合併的聯繫人?分離所有合併的聯繫人Android
回答
如果任何人有同樣的問題,我已經找到了解決方案,他們可以找到這篇文章。
private void separate_merged_contacts(){
Cursor cur1 = obj.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,new String[]{"_id"} , null, null,null);
Cursor cur_raw;
ArrayList<String> raw_contact_id = new ArrayList<String>();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
while (cur1.moveToNext()) {
raw_contact_id.clear();
ops.clear();
for (int i = 0; i < cur1.getColumnCount(); i++) {
cur_raw = obj.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts._ID}, ContactsContract.RawContacts.CONTACT_ID+"=?",new String[]{cur1.getString(cur1.getColumnIndex(ContactsContract.Contacts._ID))} , null);
while(cur_raw.moveToNext()){
for (int i = 0; i < cur_raw.getColumnCount(); i++) {
raw_contact_id.add(cur_raw.getString(cur_raw.getColumnIndexOrThrow(ContactsContract.RawContacts._ID)));
}
}
for(int i=0 ; i<raw_contact_id.size();i++){
for(int j=0;j<raw_contact_id.size();j++)
ops.add(ContentProviderOperation.newUpdate(ContactsContract.AggregationExceptions.CONTENT_URI)
.withValue(AggregationExceptions.TYPE,AggregationExceptions.TYPE_KEEP_SEPARATE)
.withValue(AggregationExceptions.RAW_CONTACT_ID1,Integer.parseInt(raw_contact_id.get(i)))
.withValue(AggregationExceptions.RAW_CONTACT_ID2,Integer.parseInt(raw_contact_id.get(j))).build());
try {
obj.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
+1用於發佈您自己發現的答案。 –
感謝您發佈您的解決方案。網絡上沒有關於aggregationExceptions使用的很多示例 –
此代碼不適用於合併聯繫人 –
儘管插入接觸本身,你可以添加更多的內容價值 您可以設置RawContacts.AGGREGATION_MODE爲RawContacts.AGGREGATION_MODE_DISABLED
有關詳細信息,請參閱: http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html
我這樣做,但它並沒有太大的幫助。我甚至在聯繫人組中禁用了aggegration,但他們仍然可以得到aggegrated :( – Roel
@DalvikVM你能發佈你的代碼嗎? – RocketRandom
中的代碼片段接受的答案(https://stackoverflow.com/a/7063235/4957915)肯定會完成工作,但我發現它有兩個問題。
1)我們正在創建不必要的條目到AggregationExceptions表。如果我們有1,000個原始聯繫人,那麼我們最終會獲得1,000,000個條目。過去幾年製造的任何手機都應該能夠在不跳動的情況下處理它,但這仍然是一種浪費。
2)更重要的是,一旦聯繫人分開,您可能會或可能不能再次合併這些聯繫人,具體取決於如何輸入條目。解決此類問題的唯一方法是刪除不可合併的聯繫人並重新創建它。
更好的方法是隻更新現有的AggregationExceptions條目。
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
// Get all entries in AggregationExceptions.
cursor = mContext.getContentResolver().query(
ContactsContract.AggregationExceptions.CONTENT_URI,
null, null, null, null);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
columnIndex = cursor.getColumnIndex(ContactsContract.AggregationExceptions.TYPE);
int type = cursor.getInt(columnIndex);
columnIndex = cursor.getColumnIndex(ContactsContract.AggregationExceptions.RAW_CONTACT_ID1);
long rawContactId1 = cursor.getLong(columnIndex);
columnIndex = cursor.getColumnIndex(ContactsContract.AggregationExceptions.RAW_CONTACT_ID2);
long rawContactId2 = cursor.getLong(columnIndex);
ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(
ContactsContract.AggregationExceptions.CONTENT_URI);
builder.withValue(ContactsContract.AggregationExceptions.TYPE,
ContactsContract.AggregationExceptions.TYPE_KEEP_SEPARATE); // <--
builder.withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID1, rawContactId1);
builder.withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, rawContactId2);
operations.add(builder.build());
}
cursor.close();
if (!operations.isEmpty()) {
try {
mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, operations);
} catch (Exception e) {
e.printStackTrace();
}
}
請注意,Android進行接觸聚合(即合併)時自動觸點彼此相似:2個觸頭具有相同的顯示名稱,例如。在這種情況下,這些合併聯繫人將不會有AggregationExceptions條目。它是自動的。
如果您使用通訊錄應用程序手動合併2個聯繫人,則會使用TYPE_KEEP_TOGETHER添加新的AggregationExceptions條目,但是沒有任何內容會更改爲原始聯繫人本身。如果您手動分開合併的聯繫人,則TYPE_KEEP_TOGETHER的條目將被標記爲已刪除,並且將添加TYPE_KEEP_SEPARATE的新條目。請記住,一旦添加了條目,它將一直存在,直到相應的聯繫人被刪除,因爲AggregationExceptions不支持「刪除」操作(https://developer.android.com/reference/android/provider/ContactsContract.AggregationExceptions.html)。
從najhi ullah的答案改進代碼的工作就像我的魅力。
ContentResolver contentResolver = getContentResolver();
if (contentResolver != null) {
Cursor contactCursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, new String[]{"_id"}, null, null, null);
if (contactCursor != null) {
ArrayList<String> rawContactIds = new ArrayList<>();
ArrayList<ContentProviderOperation> ops = new ArrayList<>();
while (contactCursor.moveToNext()) {
rawContactIds.clear();
ops.clear();
for (int i = 0; i < contactCursor.getColumnCount(); i++) {
Cursor cursorRawContact = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, new String[]{ContactsContract.RawContacts._ID}, ContactsContract.RawContacts.CONTACT_ID + "=?", new String[]{contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.Contacts._ID))}, null);
if (cursorRawContact != null) {
while (cursorRawContact.moveToNext()) {
rawContactIds.add(cursorRawContact.getString(cursorRawContact.getColumnIndexOrThrow(ContactsContract.RawContacts._ID)));
}
for (String rawContactId : rawContactIds) {
for (String rawContactId2 : rawContactIds) {
ops.add(ContentProviderOperation.newUpdate(ContactsContract.AggregationExceptions.CONTENT_URI)
.withValue(ContactsContract.AggregationExceptions.TYPE, ContactsContract.AggregationExceptions.TYPE_KEEP_SEPARATE)
.withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID1, Integer.parseInt(rawContactId))
.withValue(ContactsContract.AggregationExceptions.RAW_CONTACT_ID2, Integer.parseInt(rawContactId2)).build());
}
try {
contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
}
cursorRawContact.close();
}
}
}
contactCursor.close();
}
}
- 1. 如何獲取與給定聯繫人合併的所有聯繫人
- 2. 並非所有的聯繫人返回
- 3. Sitecore.Analytics.Exceptions.ContactLockException而合併聯繫人
- 4. Android聯繫人 - Android聯繫人的所有帳戶類型是什麼?
- 5. android獲取所有聯繫人
- 6. 如何獲取所有android聯繫人但沒有SIM卡上的聯繫人
- 7. 如何在Android中鏈接/合併/合併兩個聯繫人?
- 8. 檢索所有聯繫人時跳過最聯繫的聯繫人
- 9. Android聯繫人
- 10. 微分Outlook聯繫人(聯繫人)
- 11. Android - 從所有來源獲取所有聯繫人
- 12. Android的聯繫人
- 13. Drupal 7/CiviCRM 4.2.8 - 合併聯繫人
- 14. 合併兩個重複聯繫人/ ColdFusion
- 15. Android獲取聯繫人列表,但沒有SIM卡聯繫人
- 16. 如何在聯繫人(android)中添加聯合聯繫人圖像?
- 17. 刪除組中的所有聯繫人
- 18. autocomplete聯繫人android
- 19. 的Android不添加的所有聯繫人與重複字段
- 20. Android:對聯繫人合併作出反應
- 21. 的Android寫的聯繫人
- 22. 檢索長度爲4的所有聯繫人ID android
- 23. Android獲取ArrayList中的所有聯繫人電話號碼
- 24. 獲取Android中的所有聯繫人數據 - 多個URI
- 25. 顯示在Android中使用phonenumber的所有聯繫人列表?
- 26. 如何刪除android手機上的所有聯繫人
- 27. 如何獲得所有Android聯繫人的URI數組
- 28. 獲取聯繫人的所有號碼(電話)? (在Android 2.1)
- 29. 從Android設備獲取所有聯繫人的錯誤
- 30. 檢索所有的聯繫人照片在Android
它是一個常見的和非常知道的問題。我發現這個應用程序是有用的。它將你所有的聯繫人分開。 http://www.hanheide.net/2013/02/new-app-to-separate-accidentally-joined.html – Ubaid