下面是添加联系人数据库的代码,它还返回联系人是否被添加::::
//to save contact in Database
public boolean SaveContact(Activity _activity,String name,String number) {
String MIMETYPE_RADUTOKEN = "vnd.android.cursor.item/radutoken";
String szname = name,szMobile = number;
//Create a new contact entry!
String szToken = String.format("RADU_TOKEN_%d", System.currentTimeMillis());
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(RawContacts.ACCOUNTTYPE, null).withValue(RawContacts.ACCOUNT_NAME, null).build());
//INSERT NAME
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, szname).build());
//INSERT PINLESSMAX MOBILE NUMBER
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex).withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, szMobile).withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM).withValue(ContactsContract.Data.DATA3, "PinLessMax").build());
// SAVE CONTACT IN BCR Structure
Uri newContactUri = null;
//PUSH EVERYTHING TO CONTACTS
try{
ContentProviderResult[] res = _activity.getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops);
if (res!=null && res[0]!=null) {
newContactUri = res[0].uri;
}
}catch (RemoteException e) {
// error
newContactUri = null;
} catch (OperationApplicationException e) {
// error
newContactUri = null;
}
if (newContactUri == null) {
return false;
}
boolean foundToken = false;
// IDENTIFY Contact based on name and token
String szLookupKey = "";
Uri lkup = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, szname);
ContentResolver cr = _activity.getContentResolver();
Cursor idCursor = _activity.getContentResolver().query(lkup, null, null, null, null);
// get all the names
while (idCursor.moveToNext()) {
String szId = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts._ID));
String szName = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
szLookupKey = idCursor.getString(idCursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
// for this contact ID, search the custom field
String tokenWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] tokenWhereParams = new String[]{szId, MIMETYPE_RADUTOKEN};
Cursor tokenCur = cr.query(ContactsContract.Data.CONTENT_URI, null, tokenWhere, tokenWhereParams, null);
while (tokenCur.moveToNext()) {
String token = tokenCur.getString(tokenCur.getColumnIndex(ContactsContract.Data.DATA1));
// CHECK THE TOKEN!
if (szToken.compareTo(token) == 0) {
tokenCur.close();
foundToken = true;
break;
}
}
tokenCur.close();
if (foundToken) break;
}
idCursor.close();
return true;
}//SaveContact()