2
我一直試圖在我的聯繫人列表中添加/更新生日。我可以添加生日,但是我更新生日時遇到問題。生日數據在更新後被刪除
更新後等待幾秒鐘,生日數據消失/被刪除。
我注意到,剛剛更新的生日後,「髒」字段被設置爲「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();
}
UPDATE BIRTHDAY
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();
}
我發現問題了!由於格式不正確,數據被刪除。看起來,月份和日期字段中的前導零是重要的。我注意到的一件事是,當插入新的生日時,如果沒有前導零,則無關緊要。 String bday = year +「 - 」+ String.format(「%02d」,(monthOfYear + 1))+「 - 」+ String.format(「%02d」,dayOfMonth); – 2012-04-03 16:04:31