如何恢复联系人?

时间:2013-08-20 10:15:45

标签: java android contacts

我正在尝试使用特定联系人ID向联系人数据库添加联系人(我使用vcf文件获取联系人的详细信息),联系人ID保存在文件名(Dani_13)中,我需要添加此联系其原始条目的联系人。我一直在使用下面的代码,但它不起作用:

String [] s = (vcard_file.getName()).split(Pattern.quote("_")); 
         String [] s2 = s[(s.length)-1].split(Pattern.quote("."));
        // int rawContactInsertIndex=ops.size(); 
         int id = Integer.getInteger(s2[0]);
         ops.add(ContentProviderOperation
                 .newInsert(ContactsContract.RawContacts.CONTENT_URI)
                 .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                 .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                 .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)              
                       .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, id)
                      .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, null)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
                      .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
                      //.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
                      .build());

1 个答案:

答案 0 :(得分:0)

试试这个:

 ContentResolver cr =  getContext().getContentResolver();
             Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                     null, null, null, null);

             if (cur.getCount() > 0) {
                 while (cur.moveToNext()) {
                       String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                       String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                       if (Integer.parseInt(cur.getString(
                             cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                          Cursor pCur = cr.query(
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
                                    new String[]{id}, null);
                          while (pCur.moveToNext()) {
                              String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                          }
                         pCur.close();
                     }
                 }
             }

为我工作。