我一直在尝试在联系人列表中添加/更新生日。我可以添加生日,但是在更新生日时我遇到了问题。
更新后等待几秒钟,生日数据消失/被删除。
我注意到,在更新生日之后,“脏”字段被设置为“1”,我猜这会触发同步机制(我只是从联系人开始)
以下是我正在使用的片段
INSERT / ADD BIRTHDAY
long rawContactId = -1;
String[] projection = new String[]{ContactsContract.CommonDataKinds.Event.RAW_CONTACT_ID};
String selection = ContactsContract.CommonDataKinds.Event.CONTACT_ID + "=?";
String[] selectionArgs = new String[]{
String.valueOf(bdayContact.getId())
};
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection,
selection,
selectionArgs, null);
try {
if (c.moveToFirst()) {
rawContactId = c.getLong(0);
}
} finally {
c.close();
}
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE, bday)
.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
更新生日
String selection = ContactsContract.CommonDataKinds.Event.CONTACT_ID+"=? AND " +
ContactsContract.CommonDataKinds.Event.MIMETYPE+"=? AND " +
ContactsContract.CommonDataKinds.Event.TYPE+"=?"
;
String[] selectionArgs = new String[]{
String.valueOf(contacts.get(position).getId()),
String.valueOf(ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE),
String.valueOf(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
};
Cursor cursor = managedQuery(ContactsContract.Data.CONTENT_URI, null, selection, selectionArgs, null);
if(cursor.moveToFirst()){
int index = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event._ID);
String eventId = cursor.getString(index);
String bday = year+"-"+(monthOfYear+1)+"-"+dayOfMonth;
}
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withSelection(ContactsContract.Data._ID + " = ?", new String[] {eventId})
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE, bday)
.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
.build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:2)
我发现了问题!
由于格式错误,数据会被删除。
似乎月和日字段中的前导零非常重要。但有一件事我注意到,当插入新的生日时,如果你没有前导零则没关系。
String bday = year+"-"+String.format("%02d", (monthOfYear+1))+"-"+String.format("%02d", dayOfMonth);