Android: Create Contact API 2.x
using the API I am trying to create a contact, using the API 2.x and not the old one. Here http://developer.android.com/guide/topics/providers/content-providers.html it only explains the old API. I haven't found any proper tutorial, example, etc. showing how to create a contact. As far as I have figured out I have to create a raw contact, under raw contacts I found http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html from there I tried
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType); //accountType = "xxxx"
values.put(RawContacts.ACCOUNT_NAME, accountName); //accountName = "aaaa"
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);
The last line of code shows that "Data.CONTENT_URI" cannot be resolved. It looks a little bit that this line of code is for the 1.6 API, I have changed the Data.CONTENT_URI to ContactsContract.Data.CONTENT_URI. At least the code compiles and executes, but I still don't have a contact Mike Sullivan in my addressbook afterwards. I exchanged now as well the other "Data" with "ContactsContract.Data" still no changes.
Has anyone an easy example how to create a person in the addressbook on 2.x?
Edit: I made some progress, it looks like that I always need an account on my phone to add a contact. My phone has account type com.google and account name xxxxx@gmail.com. The emulator has 开发者_如何学运维nothing. I wonder to which account I have to add my contacts? Can I assume that I ALWAYS have exactly one gmail account and take this one?
Are you looking for something like this?
{ // insert a new data item
// first we need to get a raw contact corresponding to the contact.
Cursor rawCur = getContentResolver().query(RawContacts.CONTENT_URI,
new String[]{RawContacts._ID},
RawContacts.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)}, null);
long rawContactId = -1;
for (boolean moreRaws = rawCur.moveToFirst(); moreRaws;
moreRaws = rawCur.moveToNext())
{
rawContactId = rawCur.getLong(rawCur.getColumnIndex(RawContacts._ID));
}
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"y123-456-7890")
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME)
.build());
try {
ContentProviderResult[] results =
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
logger.info("result: "+results[0]);
} catch (UnsupportedOperationException ex) {
ex.printStackTrace();
} catch (RemoteException ex) {
ex.printStackTrace();
} catch (OperationApplicationException ex) {
ex.printStackTrace();
}
}
精彩评论