如何找出日历意图的结果?

Cro*_*ile 11 android calendar google-calendar-api android-intent

从我的应用程序,我发布了一个意图的日历:

    Calendar cal = Calendar.getInstance();              
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", true);
    intent.putExtra("rrule", "FREQ=YEARLY");
    intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
    intent.putExtra("title", "Some title");
    startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

如果用户继续并保存这个预先填充的日历条目,我无法弄清楚如何获取eventID.我还想知道用户是否取消了日历提示,并且没有保存这个新的预先填充的事件.

是否有任何相关的返回onActivityResult(...)我可以用作日历事件的参考?我需要这个,所以我可以稍后查找/打开日历事件进行查看/编辑. [更新:]是的,尝试onActivityResult(...),一旦日历在任何用户交互之前打开,意图就会返回,所以这没用.

我想通过使用意图(也让用户从设备上可用的各种日历中选择)切换到日历应用程序并避免从我的应用程序重新创建日历UE来实现此目的.我也想支持Android 2.2+.

小智 8

这就是我做的:

我在开始意图之前得到了下一个事件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;
}
Run Code Online (Sandbox Code Playgroud)

然后,我像往常一样调用意图:

        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);
Run Code Online (Sandbox Code Playgroud)

这就是诀窍,在我的活动的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
    }
}
Run Code Online (Sandbox Code Playgroud)


Ale*_*sky 3

我认为您可以通过 onResume 方法中的 beginTime 检查事件。绝对不会有两个事件具有相同的开始时间。