插入联系人的Andr​​oid 2.2联系人、Andr、oid

2023-09-12 00:32:08 作者:傷芣起づ

我想插入新的 RawContact 接触,但 RawContact 加时,我认为不会被显示出来通过联系人或通讯录中的联系人。据我了解,如果我们创建一个 RawContact 并没有与它相关联的接触则触点将被自动插入。我得到 rawContactId 的有效值,并没有异常抛出,所以我想插入成功。难道我做错什么还是我失去了一些东西?我使用的开发者网站上的code为例,只需将其粘贴在这里:

  ContentValues​​值=新ContentValues​​();
 values​​.put(RawContacts.ACCOUNT_TYPE,accountType);
 values​​.put(RawContacts.ACCOUNT_NAME,帐户名);
 乌里rawContactUri = getContentResolver()插入(RawContacts.CONTENT_URI,值)。
 长rawContactId = ContentUris.parseId(rawContactUri);

 values​​.clear();
 values​​.put(Data.RAW_CONTACT_ID,rawContactId);
 values​​.put(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE);
 values​​.put(StructuredName.DISPLAY_NAME,迈克·沙利文);
 。getContentResolver()插入(Data.CONTENT_URI,价值观);
 

解决方案

我觉得这种问答是早已被人遗忘,但既然有人upvoted它,我假设其他人也面临同样的问题,因为我的。有点挣扎后,我能找出问题并插入接触,希望这会有所帮助,这里是样本code:

 的ArrayList< ContentProviderOperation> OPS =新的ArrayList< ContentProviderOperation>();
INT rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(RawContacts.ACCOUNT_TYPE,空)
   .withValue(RawContacts.ACCOUNT_NAME,空)
   。建立());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
   .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
   .withValue(Phone.NUMBER,9X-XXXXXXXXX)
   。建立());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(Data.RAW_CONTACT_ID,rawContactInsertIndex)
   .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
   .withValue(StructuredName.DISPLAY_NAME,迈克·沙利文)
   。建立());
ContentProviderResult [] RES = getContentResolver()applyBatch(ContactsContract.AUTHORITY,OPS)。
 

I am trying to insert new RawContact contacts, but the RawContact added doesn't get displayed when I view the contacts through Contacts or phonebook. As I understand if we create a RawContact and there is no contact associated with it then the contact will be automatically inserted. I get a valid value of rawContactId and no exceptions are thrown, so I assume the insertion is successful. Am I doing anything wrong or am I missing something? I am using the code example from developer site, just pasting it here:

 ContentValues values = new ContentValues();
 values.put(RawContacts.ACCOUNT_TYPE, accountType); 
 values.put(RawContacts.ACCOUNT_NAME, accountName);
 Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values); 
 long rawContactId = ContentUris.parseId(rawContactUri); 

 values.clear(); 
 values.put(Data.RAW_CONTACT_ID, rawContactId); 
 values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); 
 values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan"); 
 getContentResolver().insert(Data.CONTENT_URI, values); 

解决方案

I thought this Q was long forgotten but Since someone upvoted it, I am assuming someone else also faces the same problem as me. After a little struggle I was able to figure out the problem and insert contacts, Hope this helps, here is the sample code:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
   .withValue(RawContacts.ACCOUNT_TYPE, null)
   .withValue(RawContacts.ACCOUNT_NAME,null )
   .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
   .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
   .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
   .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
   .withValue(Data.MIMETYPE,StructuredName.CONTENT_ITEM_TYPE)
   .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
   .build());  
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);