这就是我所做的:
我在开始意图之前获取下一个事件 ID:
public static long getNewEventId(ContentResolver cr) {
Cursor cursor = cr.query(Events.CONTENT_URI, new String [] {"MAX(_id) as max_id"}, null, null, "_id");
cursor.moveToFirst();
long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));
return max_val+1;
}
然后,我像往常一样调用意图:
long event_id = EventUtility.getNewEventId(mContext.getContentResolver());
Intent intent = new Intent(Intent.ACTION_INSERT)
.setData(Events.CONTENT_URI)
.putExtra(Events._ID, event_id)
.putExtra(Events.TITLE, "title");
startActivity(intent);
这就是技巧,在我的 Activity 的 onResume() 中,我检查是否创建了与之前生成的 event_id 相同的新 event_id。如果相同,则表示新日历已创建。然后,我将新内容存储在我的数据库中。
public static long getLastEventId(ContentResolver cr) {
Cursor cursor = cr.query(Events.CONTENT_URI, new String [] {"MAX(_id) as max_id"}, null, null, "_id");
cursor.moveToFirst();
long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));
return max_val;
}
@Override
public void onResume() {
super.onResume();
long prev_id = EventUtility.getLastEventId(getContentResolver());
// if prev_id == mEventId, means there is new events created
// and we need to insert new events into local sqlite database.
if (prev_id == mEventID) {
// do database insert
}
}