在Calendar Provider中创建新事件

时间:2014-03-29 11:10:02

标签: java android calendar

所以我在几毫秒内使用日期,我可以在特定时间创建通知,但我也想在用户日历中添加事件。我在这里找到了一些信息http://developer.android.com/guide/topics/providers/calendar-provider.html 但我仍然在努力。例如:如何获取用户帐户名?

编辑: 所以,我发现了这个:

    Calendar beginTime = Calendar.getInstance();
    beginTime.setTimeInMillis(when);
    Calendar endTime = Calendar.getInstance();
    endTime.setTimeInMillis(when+3600000);
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);
 startActivity(intent);

它完全适合我,但它会打开一个calentdar窗口来确认添加事件。有没有办法避免这种情况?

EDIT2:找到了我要找的东西:

    long calID = 3;
    ContentResolver cr = getContentResolver();
    ContentValues values = new ContentValues();
    values.put(Events.DTSTART, when);
    values.put(Events.DTEND, when+3600000);
    values.put(Events.TITLE, "Jazzercise");
    values.put(Events.DESCRIPTION, "Group workout");
    values.put(Events.CALENDAR_ID, calID);
    values.put(Events.EVENT_TIMEZONE, "America/Los_Angeles");
    cr.insert(Events.CONTENT_URI, values);

3 个答案:

答案 0 :(得分:4)

您可以尝试使用此代码将事件插入日历

public static long pushEventToCalender(Activity curActivity,
            String title, String addInfo, String place, int status,
            long startDate, int reminderTime, boolean needReminder,
            boolean needMailService) {
        /***************** Event: note(without alert) *******************/

        String eventUriString = "content://com.android.calendar/events";
        ContentValues eventValues = new ContentValues();

        eventValues.put("calendar_id", 1); // id, We need to choose from
                                            // our mobile for primary
                                            // its 1
        eventValues.put("title", title);
        eventValues.put("description", addInfo);
        eventValues.put("eventLocation", place);

        long endDate = startDate + 1000 * 60 * 60; // For next 1hr

        eventValues.put("dtstart", startDate);
        eventValues.put("dtend", endDate);

        // values.put("allDay", 1); //If it is bithday alarm or such
        // kind (which should remind me for whole day) 0 for false, 1
        // for true
        eventValues.put("eventStatus", status); // This information is
        // sufficient for most
        // entries tentative (0),
        // confirmed (1) or canceled
        // (2):
        /*
         * eventValues.put("visibility", 3); // visibility to default (0), //
         * confidential (1), private // (2), or public (3):
         */
        // eventValues.put("transparency", 0); // You can control whether
        // an event consumes time
        // opaque (0) or transparent
        // (1).
        eventValues.put("hasAlarm", 1); // 0 for false, 1 for true

        eventValues.put("eventTimezone", TimeZone.getDefault().getID());
        Uri eventUri = curActivity.getApplicationContext().getContentResolver()
                .insert(Uri.parse(eventUriString), eventValues);
        long eventID = Long.parseLong(eventUri.getLastPathSegment());

        if (needReminder) {
            /***************** Event: Reminder(with alert) Adding reminder to event *******************/

            String reminderUriString = "content://com.android.calendar/reminders";

            ContentValues reminderValues = new ContentValues();

            reminderValues.put("event_id", eventID);
            reminderValues.put("minutes", reminderTime); // Default value of the
            // system. Minutes is a
            // integer
            reminderValues.put("method", 1); // Alert Methods: Default(0),
                                                // Alert(1), Email(2),
                                                // SMS(3)

            Uri reminderUri = curActivity.getApplicationContext()
                    .getContentResolver()
                    .insert(Uri.parse(reminderUriString), reminderValues);
        if(_DEBUG)
            Log.e("URI", reminderUri.toString());
        }

        /***************** Event: Meeting(without alert) Adding Attendies to the meeting *******************/

        if (needMailService) {
            String attendeuesesUriString = "content://com.android.calendar/attendees";

            /********
             * To add multiple attendees need to insert ContentValues multiple
             * times
             ***********/
            ContentValues attendeesValues = new ContentValues();

            attendeesValues.put("event_id", eventID);
            attendeesValues.put("attendeeName", "xxxxx"); // Attendees name
            attendeesValues.put("attendeeEmail", "yyyy@gmail.com");// Attendee
                                                                    // E
                                                                    // mail
                                                                    // id
            attendeesValues.put("attendeeRelationship", 0); // Relationship_Attendee(1),
                                                            // Relationship_None(0),
                                                            // Organizer(2),
                                                            // Performer(3),
                                                            // Speaker(4)
            attendeesValues.put("attendeeType", 0); // None(0), Optional(1),
                                                    // Required(2), Resource(3)
            attendeesValues.put("attendeeStatus", 0); // NOne(0), Accepted(1),
                                                        // Decline(2),
                                                        // Invited(3),
                                                        // Tentative(4)

            Uri attendeuesesUri = curActivity.getApplicationContext()
                    .getContentResolver()
                    .insert(Uri.parse(attendeuesesUriString), attendeesValues);
            if(_DEBUG)
                Log.e("URI", attendeuesesUri.toString());
        }

        return eventID;

    }

希望这会对你有所帮助

答案 1 :(得分:0)

尝试使用以下代码获取日历帐户名称

Uri uri;
String calName,calId,calAccName,calType;

        if (Build.VERSION.SDK_INT >= 8) uri = Uri.parse("content://com.android.calendar/calendars");
        else uri = Uri.parse("content://calendar/calendars");

Cursor cursor = managedQuery(uri, null, null, null, null);  //all calendars

cursor.moveToFirst();
while (!cursor.isAfterLast())
{
    calName = cursor.getString(cursor.getColumnIndex(projection[1]));
   if(calName.equalsIgnoreCase(clsCommon.CALENDAR_NAME))
{
    flag= true;
    calId = cursor.getString(cursor.getColumnIndex(projection[0]));
    calAccName = cursor.getString(cursor.getColumnIndex(projection[2]));
    calType = cursor.getString(cursor.getColumnIndex(projection[3]));
}
    cursor.moveToNext();
}

修改

要将日历活动添加到日历,请尝试以下代码

Uri calUri = CalendarContract.Calendars.CONTENT_URI; 
ContentValues cv = new ContentValues();
cv.put(CalendarContract.Calendars.ACCOUNT_NAME, UserName);
cv.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
cv.put(CalendarContract.Calendars.NAME, "NAME");
cv.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "Name");
cv.put(CalendarContract.Calendars.CALENDAR_COLOR, getResources().getColor(R.color.app_default));
cv.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
cv.put(CalendarContract.Calendars.OWNER_ACCOUNT, true);
cv.put(CalendarContract.Calendars.VISIBLE, 1);
cv.put(CalendarContract.Calendars.SYNC_EVENTS, 1);

calUri = calUri.buildUpon()
                        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, UserName)
                        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL).build();

getContentResolver().insert(calUri, cv);

答案 2 :(得分:0)

您不需要编写所有这些代码,请参阅以下代码,它将对您有所帮助,

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