1

我使用此代码添加原始联系人

ArrayList<ContentProviderOperation> ops =
  new ArrayList<ContentProviderOperation>();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, null)
.withValue(RawContacts.ACCOUNT_NAME, null)
.withValue(RawContacts.AGGREGATION_MODE,  RawContacts.AGGREGATION_MODE_DEFAULT)
.build());

....
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

如果我多次添加相同的信息,在 android 2.1 和 2.2 中,所有原始联系人都会出现在一个联系人下。但是在 4.03 和 4.1 中,它们都显示为独立的联系人,尽管它们是相同的。

这是系统的错误还是功能?还是只是因为我错过了什么?

4

1 回答 1

0

试试下面的代码:

1)在单击关闭按钮上尝试以下操作:

       Intent in = new Intent(Intent.ACTION_INSERT);
        in.setType(ContactsContract.Contacts.CONTENT_TYPE);
        startActivityForResult(in,ADD_DATA);

2) ONActivityResult 方法:

     @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case ADD_DATA:
        if(resultCode == RESULT_OK) {

              Uri contactData = data.getData();
                Cursor cur = managedQuery(contactData, null, null, null, null);
                ContentResolver contect_resolver = getContentResolver();

                if (cur.moveToFirst()) {
                    String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String lookupkey = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY));
                    String name = "";
                    String no = "";

                    Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

                    if (phoneCur.moveToFirst()) {
                        name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String lookup = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LOOKUP_KEY));
                    System.out.println("Contact Name"+name);
                    System.out.println("Number"+no);





                    id = null;
                    name = null;
                    no = null;
                    phoneCur = null;

                contect_resolver = null;
                cur = null;
            }
       }
}

    if(resultCode == RESULT_CANCELED)
        {

        }

        break;
    default:
        break;
    }   
   }
于 2012-10-03T04:11:46.683 回答