0

Hi I am using the following Code to add a Event to my Android Calendar using Intent.

Calendar cal = Calendar.getInstance();
        Intent intent = new Intent(Intent.ACTION_INSERT); // Even Tried Intent.ACTION_EDIT
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", cal.getTimeInMillis());
        intent.putExtra("allDay", false);
        intent.putExtra("rrule", "FREQ=DAILY");
        intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
        intent.putExtra("title", mtodoText);
        intent.putExtra(CalendarContract.Events.CALENDAR_ID,3);
        startActivity(intent);

But the issue is that this event is not saved or Created to the Calendar_Id = 3, but inspite that it is created to the calendar_Id = 1. I am using the following code to retrieve all of my Calendar Events, And if I check the log cat output it shows the event is created in Calendar_Id 1.

Uri EVENTS_URI = Uri.parse(getCalendarUriBase(true) + "events");
        String sSelection = "((" +  CalendarContract.Events.DELETED + " != 1))";

        /*String sSelection = "((" + CalendarContract.Events.CALENDAR_ID + " = 3 ) AND ("
                +  CalendarContract.Events.DELETED + " != 1))";*/

        cur = getActivity().getContentResolver().query(EVENTS_URI, null, sSelection, null, "_id");

        if(cur!=null){
            while (cur.moveToNext()) {
                String taskname=cur.getString(cur.getColumnIndex(CalendarContract.Events.TITLE));
                String taskdate=cur.getString(cur.getColumnIndex(CalendarContract.Events.DTSTART));
                String s_id=cur.getString(cur.getColumnIndex(CalendarContract.Events._ID));
                String taskstatusflag=cur.getString(cur.getColumnIndex(CalendarContract.Events.HAS_ALARM));

                Log.e("Title:", "taskname: " + taskname);
                Log.e("DTSTART","taskdate: " + taskdate);
                Log.e("_ID","s_Id: "+ s_id);
                Log.e("HAS ALARM", "taskstatusflag: " + taskstatusflag);
                Log.e("Calender ID:",cur.getString(cur.getColumnIndex(CalendarContract.Events.CALENDAR_ID))+"");
}
}

My Logcat Out is as Follows:-

05-31 17:17:54.039    6518-6518/com.myapp E/Title:﹕ taskname: test1
        05-31 17:17:54.039    6518-6518/com.myapp E/DTSTART﹕ taskdate: 1464608459000
        05-31 17:17:54.039    6518-6518/com.myapp E/_ID﹕ s_Id: 22162
        05-31 17:17:54.039    6518-6518/com.myapp E/HAS ALARM﹕ taskstatusflag: 0
        05-31 17:17:54.039    6518-6518/com.myapp E/Calender ID:﹕ 1
        05-31 17:17:54.039    6518-6518/com.myapp E/Title:﹕ taskname: test Cal 3
        05-31 17:17:54.039    6518-6518/com.myapp E/DTSTART﹕ taskdate: 1464684678000
        05-31 17:17:54.039    6518-6518/com.myapp E/_ID﹕ s_Id: 22163
        05-31 17:17:54.039    6518-6518/com.myapp E/HAS ALARM﹕ taskstatusflag: 0
        05-31 17:17:54.039    6518-6518/com.myapp E/Calender ID:﹕ 1
        05-31 17:17:54.039    6518-6518/com.myapp E/Title:﹕ taskname: test guest
        05-31 17:17:54.039    6518-6518/com.myapp E/DTSTART﹕ taskdate: 1464694253000
        05-31 17:17:54.039    6518-6518/com.myapp E/_ID﹕ s_Id: 22164
        05-31 17:17:54.039    6518-6518/com.myapp E/HAS ALARM﹕ taskstatusflag: 0
        05-31 17:17:54.039    6518-6518/com.myapp E/Calender ID:﹕ 1
        05-31 17:17:54.039    6518-6518/com.myapp E/Title:﹕ taskname: test Cal 3
        05-31 17:17:54.039    6518-6518/com.myapp E/DTSTART﹕ taskdate: 1464694676000
        05-31 17:17:54.039    6518-6518/com.myapp E/_ID﹕ s_Id: 22165
        05-31 17:17:54.039    6518-6518/com.myapp E/HAS ALARM﹕ taskstatusflag: 0
        05-31 17:17:54.039    6518-6518/com.myapp E/Calender ID:﹕ 1
        05-31 17:17:54.039    6518-6518/com.myapp E/Title:﹕ taskname: test Cal 33
        05-31 17:17:54.039    6518-6518/com.myapp E/DTSTART﹕ taskdate: 1464695268000
        05-31 17:17:54.039    6518-6518/com.myapp E/_ID﹕ s_Id: 22166
        05-31 17:17:54.039    6518-6518/com.myapp E/HAS ALARM﹕ taskstatusflag: 0
        05-31 17:17:54.039    6518-6518/com.myapp E/Calender ID:﹕ 1

What should I do to explicitly create the event in Calendar_Id = 3, Using the Intent.

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50

1 Answers1

1

What you're trying to do is not possible. The user will always be able to change the calendar selection when you launch the Intent to create an event (just like he can change every other value of the event before storing it).

There is a workaround though. You can create a placeholder event and call an Intent to edit it. Many calendar apps don't allow to move existing events to another calendar, so this would do the trick (unless the user uses a 3rd party calendar app that supports moving the event to another calendar).

I've outlined this workaround in Specify calendar in Android event creation intent. Btw, this question is the first result when I google the title of your question, which might explain the downvote.

Community
  • 1
  • 1
Marten
  • 3,802
  • 1
  • 17
  • 26