2011-08-12 21 views
2

我正在使用Outlook Express創建藍牙同步應用程序。所有的工作都做得非常好,但我還有一個小問題。當我將我的聯繫人從Outlook同步到Android時,它會合並具有相似名稱的聯繫人。例如,如果我有兩個名爲「Najhi」和「Najhi Ullah」的聯繫人,那麼在同步後,他們將以Android名稱「Najhi」合併。有沒有解決方案以編程方式分離所有合併的聯繫人?分離所有合併的聯繫人Android

+1

它是一個常見的和非常知道的問題。我發現這個應用程序是有用的。它將你所有的聯繫人分開。 http://www.hanheide.net/2013/02/new-app-to-separate-accidentally-joined.html – Ubaid

回答

4

如果任何人有同樣的問題,我已經找到了解決方案,他們可以找到這篇文章。

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(); 
       } 
     } 
    } 
} 
+0

+1用於發佈您自己發現的答案。 –

+0

感謝您發佈您的解決方案。網絡上沒有關於aggregationExceptions使用的很多示例 –

+0

此代碼不適用於合併聯繫人 –

1

儘管插入接觸本身,你可以添加更多的內容價值 您可以設置RawContacts.AGGREGATION_MODE爲RawContacts.AGGREGATION_MODE_DISABLED

有關詳細信息,請參閱: http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html

+0

我這樣做,但它並沒有太大的幫助。我甚至在聯繫人組中禁用了aggegration,但他們仍然可以得到aggegrated :( – Roel

+0

@DalvikVM你能發佈你的代碼嗎? – RocketRandom

0

中的代碼片段接受的答案(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)。

0

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(); 
     } 
    } 
相關問題