How to add calendar events in Android?

前端 未结 10 1216
太阳男子
太阳男子 2020-11-22 13:31

I\'m just getting up to speed on Android, and today in a project meeting someone said that Android has no native calendar app so users just use whatever calendar app they li

相关标签:
10条回答
  • 2020-11-22 14:29

    Try this ,

       Calendar beginTime = Calendar.getInstance();
        beginTime.set(yearInt, monthInt - 1, dayInt, 7, 30);
    
    
    
        ContentValues l_event = new ContentValues();
        l_event.put("calendar_id", CalIds[0]);
        l_event.put("title", "event");
        l_event.put("description",  "This is test event");
        l_event.put("eventLocation", "School");
        l_event.put("dtstart", beginTime.getTimeInMillis());
        l_event.put("dtend", beginTime.getTimeInMillis());
        l_event.put("allDay", 0);
        l_event.put("rrule", "FREQ=YEARLY");
        // status: 0~ tentative; 1~ confirmed; 2~ canceled
        // l_event.put("eventStatus", 1);
    
        l_event.put("eventTimezone", "India");
        Uri l_eventUri;
        if (Build.VERSION.SDK_INT >= 8) {
            l_eventUri = Uri.parse("content://com.android.calendar/events");
        } else {
            l_eventUri = Uri.parse("content://calendar/events");
        }
        Uri l_uri = MainActivity.this.getContentResolver()
                .insert(l_eventUri, l_event);
    
    0 讨论(0)
  • 2020-11-22 14:35

    Just in case if someone needs this for Xamarin in c#:

            Intent intent = new Intent(Intent.ActionInsert);
            intent.SetData(Android.Provider.CalendarContract.Events.ContentUri);
            intent.PutExtra(Android.Provider.CalendarContract.ExtraEventBeginTime, Utils.Tools.CurrentTimeMillis(game.Date));
            intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.AllDay, false);
            intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.EventLocation, "Location");
            intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.Description, "Description");
            intent.PutExtra(Android.Provider.CalendarContract.ExtraEventEndTime, Utils.Tools.CurrentTimeMillis(game.Date.AddHours(2)));
            intent.PutExtra(Android.Provider.CalendarContract.EventsColumns.Title, "Title");
            StartActivity(intent);
    

    Helper Functions:

        private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    
        public static long CurrentTimeMillis(DateTime date)
        {
            return (long)(date.ToUniversalTime() - Jan1st1970).TotalMilliseconds;
        }
    
    0 讨论(0)
  • 2020-11-22 14:36

    if you have a given Date string with date and time .

    for e.g String givenDateString = pojoModel.getDate()/* Format dd-MMM-yyyy hh:mm:ss */

    use the following code to add an event with date and time to the calendar

    Calendar cal = Calendar.getInstance();
    cal.setTime(new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss").parse(givenDateString));
    Intent intent = new Intent(Intent.ACTION_EDIT);
    intent.setType("vnd.android.cursor.item/event");
    intent.putExtra("beginTime", cal.getTimeInMillis());
    intent.putExtra("allDay", false);
    intent.putExtra("rrule", "FREQ=YEARLY");
    intent.putExtra("endTime",cal.getTimeInMillis() + 60 * 60 * 1000);
    intent.putExtra("title", " Test Title");
    startActivity(intent);
    
    0 讨论(0)
  • 2020-11-22 14:37

    i used the code below, it solves my problem to add event in default device calendar in ICS and also on version less that ICS

        if (Build.VERSION.SDK_INT >= 14) {
            Intent intent = new Intent(Intent.ACTION_INSERT)
            .setData(Events.CONTENT_URI)
            .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis())
            .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis())
            .putExtra(Events.TITLE, "Yoga")
            .putExtra(Events.DESCRIPTION, "Group class")
            .putExtra(Events.EVENT_LOCATION, "The gym")
            .putExtra(Events.AVAILABILITY, Events.AVAILABILITY_BUSY)
            .putExtra(Intent.EXTRA_EMAIL, "rowan@example.com,trevor@example.com");
             startActivity(intent);
    }
    
        else {
            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", "A Test Event from android app");
            startActivity(intent);
            }
    

    Hope it would help.....

    0 讨论(0)
提交回复
热议问题