在Android M中添加日历事件的问题

时间:2016-07-19 10:18:51

标签: android google-calendar-api android-6.0-marshmallow

我正在使用代码将事件添加到设备的日历中。该代码在除Android M之外的所有Android版本中都能正常运行。在Android M上添加的事件显示为生日而不是事件。请帮助解决如何解决此问题。

使用的代码如下

     // Add event to calendar
        try {
            ContentResolver cr = context.getContentResolver();
            ContentValues values = new ContentValues();
            values.put(CalendarContract.Events.DTSTART, startDateInMilliSeconds);
            values.put(CalendarContract.Events.DTEND, endDateInMilliSeconds);
            values.put(CalendarContract.Events.TITLE, title);
            values.put(CalendarContract.Events.DESCRIPTION, description);
            if (location != null && (!TextUtils.isEmpty(location))) {
                values.put(CalendarContract.Events.EVENT_LOCATION, location);
            }
            values.put(CalendarContract.Events.CALENDAR_ID, 1);
            values.put(CalendarContract.Events.EVENT_TIMEZONE, Calendar.getInstance()
                    .getTimeZone().getID());             
            Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
            // Save the eventId into the Task object for possible future delete.
            long eventId = Long.parseLong(uri.getLastPathSegment());            
            Uri openCalendarEvent = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventId);
            Intent calIntent = new Intent(Intent.ACTION_VIEW).setData(uri);
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, startDateInMilliSeconds);
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endDateInMilliSeconds);
            calIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(calIntent);              

        } catch (Exception e) {
            e.printStackTrace();

        }

1 个答案:

答案 0 :(得分:0)

尝试以下方法@ Himmat

public void addReminder() {
        Intent calIntent = new Intent(Intent.ACTION_EDIT);
        calIntent.setData(CalendarContract.Events.CONTENT_URI);
        calIntent.setType("vnd.android.cursor.item/event");
        calIntent.putExtra(CalendarContract.Events.TITLE, e.getName());
        calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, e.getLocation());
        calIntent.putExtra(CalendarContract.Events.DESCRIPTION, e.getDescription());
        Calendar calStart=Calendar.getInstance();
        if(Validator.isNotNull(e.getStartDate())) {
            calStart.setTime(e.getStartDate());
        }
        calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                calStart.getTimeInMillis());
        Calendar calEnd=Calendar.getInstance();
        if(Validator.isNotNull(e.getEndDate())) {
            calEnd.setTime(e.getEndDate());
        }
        calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                calEnd.getTimeInMillis());
        calIntent.putExtra(CalendarContract.EXTRA_CUSTOM_APP_URI,
                e.getRegistrationLink());
        startActivity(calIntent);
    }