分离所有合并的 Android 联系人

naj*_*lah 2 outlook android contacts android-contacts

我正在使用 Outlook Express 创建蓝牙同步应用程序。所有工作都完成得非常好,但我还剩下一个小问题。当我将联系人从 Outlook 同步到 Android 时,它会合并具有相似名称的联系人。例如,如果我有两个名为“Najhi”和“Najhi Ullah”的联系人,那么同步后,它们将在 Android 中合并到一个名称“Najhi”下。是否有任何解决方案可以以编程方式分离所有合并的联系人?

naj*_*lah 5

我自己找到了解决方案,如果有人有同样的问题,他们可以找到这篇文章。

  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();
                }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)