如何创建从今天起X天结束的Android日历活动

时间:2014-03-28 07:48:42

标签: java android android-intent calendar android-activity

我正在开发一款具有后续功能的Android应用。

人们将能够提供一些信息,包括描述和他们希望此活动开始的未来天数(IE 1天,10天,20天),然后单击创建按钮进行创建。

我遇到的问题是我希望日期在未来X天开始,其中X是将来开始的天数(天数为1天,IE 1天,10天)天)。

这是我的代码:

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

}

终于搞定了!粗体代码是缺少的。感谢他们的想法,因为他们带我到正确的代码!

1 个答案:

答案 0 :(得分:0)

简单代码:

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