简体   繁体   中英

How to create an Android Calendar event that ends X days from today

I am working on an android app that has a follow-up feature.

People will be able to put in some information including a description and the number of days in the future they want this event to start (IE 1 day, 10 days, 20 days) and then click the create button to create it.

The problem I am having is that I want the date to start X number of days in the future where X is the number of days in the future where it would start(an int passed in of days, IE 1 day, 10 days).

Here is my code:

//Create Calendar event
public void CreateEvent(){
    //Variables title, location, description, and days_from_now are all defined elsewhere
    Calendar mycal = Calendar.getInstance();

    Intent calIntent = new Intent(Intent.ACTION_INSERT);
    calIntent.setType("vnd.android.cursor.item/event");

    //Put information in
    calIntent.putExtra(Events.TITLE, title); 
    calIntent.putExtra(Events.EVENT_LOCATION, location); 
    calIntent.putExtra(Events.DESCRIPTION, description);

    **//Increment the date by X days
    mycal.add(mycal.DATE, days_from_now );

    //Start and end time
    long startTime = mycal.getTimeInMillis(); //Convert to milliseconds
    long endTime = startTime+900000; //15 minutes

    //Put the calculated start and end time into the calIntent Intent
    calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startTime);
    calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime);**

    startActivity(calIntent); 

}

Finally got it working! The bolded code is what was missing. Thanks for the ideas guys as they led me to the right code!

simple code:

public void addEvent(long calendarID, String title, long startEvent,
            long endEvent, int reminder, String description, long createDate) {
        try {
            System.out.println("add event to calendar");
            ContentResolver cr = context.getContentResolver();
            ContentValues values = new ContentValues();
            values.put(Events.DTSTART, startEvent);
            values.put(Events.DTEND, endEvent);
            values.put(Events.TITLE, title);
            values.put(Events.DESCRIPTION, description);
            values.put(Events.CALENDAR_ID, calendarID);
            values.put(Events.EVENT_TIMEZONE, Calendar.getInstance()
                    .getTimeZone().getID());
            System.out.println(Calendar.getInstance().getTimeZone().getID());
            Uri uri = cr.insert(Events.CONTENT_URI, values);
            long eventId = Long.parseLong(uri.getLastPathSegment());
            setReminder(cr, eventId, reminder);             
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setReminder(ContentResolver cr, long eventID, int reminder) {
        try {
            ContentValues values = new ContentValues();
            values.put(Reminders.MINUTES, reminder / 60000);
            values.put(Reminders.EVENT_ID, eventID);
            values.put(Reminders.METHOD, Reminders.METHOD_ALERT);
            Uri uri = cr.insert(Reminders.CONTENT_URI, values);
            Cursor c = Reminders.query(cr, eventID,
                    new String[] { Reminders.MINUTES });
            if (c.moveToFirst()) {
                System.out.println("calendar"
                        + c.getInt(c.getColumnIndex(Reminders.MINUTES)));
            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM