Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out the result of my calendar intent?

From my app I am launching the calendar with an 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);

I cannot figure out how to get the eventID back if the user goes ahead and saves this pre-populated calendar entry. I also want to know if the user cancelled the calendar prompt, and did not save this new pre-populated event.

Is there anything relevant returned to onActivityResult(...) which I could use as a reference to the calendar event? I need this, so I can later find/open the calendar event for viewing/editing. [Update:] Yea, tried onActivityResult(...), and the intent returns as soon as the calendar opens before any user interaction, so this is no use.

I would like to do this by handing off to the calendar application by using an intent(also to let the user select from the various calendars available on the device) and avoid recreating the calendar UE from my app. Also I would like to support Android 2.2+ at the very least.

like image 242
Crocodile Avatar asked Mar 18 '12 19:03

Crocodile


2 Answers

I think you can check event by beginTime in onResume method. There definitely won't be 2 events with the same beginTime.

like image 81
Alex Klimashevsky Avatar answered Nov 01 '22 20:11

Alex Klimashevsky


This is what I do:

I get the next Event Id before starting the intent:

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;
}

Then, I call the intent like usual:

        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);

And this is the trick, in onResume() of my activity I check if there is new event_id created same as the event_id generated previously. If they are same, means the new calendar has been created. Then, I store the new on my database.

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
    }
}
like image 28
yodann Avatar answered Nov 01 '22 21:11

yodann