0

如果用户愿意,我需要在默认的 android 日历中添加一个重复事件并对其进行修改。我需要直接执行此操作,即无需打开日历意图。我使用了以下代码。Toast 显示最后添加的事件的事件 ID,但该事件未显示在电话日历中。真的很困扰,但无法解决问题..请帮助..

 public void createEvent(String title, String location, String description){

    Calendar cal = Calendar.getInstance();
     cal.setTimeZone(TimeZone.getTimeZone("GMT-1"));
     Date dt = null;
     Date dt1 = null;
     String Stime="2013-11-13 07:30";
     String Etime="2013-11-13 08:00";
     try {
      dt = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(Stime);
      dt1 = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(Etime);

      Calendar beginTime = Calendar.getInstance();
      cal.setTime(dt);

      // beginTime.set(2013, 7, 25, 7, 30);
      beginTime.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
      cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY),
      cal.get(Calendar.MINUTE));

      Calendar endTime = Calendar.getInstance();
      cal.setTime(dt1);

      // endTime.set(2013, 7, 25, 14, 30);
      // endTime.set(year, month, day, hourOfDay, minute);
      endTime.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
      cal.get(Calendar.DATE), cal.get(Calendar.HOUR_OF_DAY),
      cal.get(Calendar.MINUTE));

      ContentResolver cr = this.getContentResolver();
      ContentValues values = new ContentValues();

      values.put(Events.DTSTART, beginTime.getTimeInMillis());
      values.put(Events.DTEND, endTime.getTimeInMillis());
      values.put(Events.TITLE, title);
      values.put(Events.DESCRIPTION, description);
      values.put(Events.CALENDAR_ID, 3 );
      // values.put(Events._ID, meeting_id);
      values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

      Uri uri = cr.insert(Events.CONTENT_URI, values);
      long eventID = Long.parseLong(uri.getLastPathSegment());

        Toast toast =Toast.makeText(getApplicationContext(), "Calender Event Added" + eventID,Toast.LENGTH_LONG);
        toast.show();

     } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
    }

另外我必须在低于 14 的 android 版本中使用相同的东西。我应该用什么更新代码?谢谢..

4

2 回答 2

1

我使用了以下代码,最终在默认电话日历中显示了事件,但仍然想知道我是否必须在 >14 版本中使用此代码。我应该更新什么?

public void createEvent(String title, String location, String description, String startDate, String endDate)
{

    Calendar calendarStart = CalendarPlugin.getCalendarFromISO(startDate);
    Calendar calendarEnd = CalendarPlugin.getCalendarFromISO(endDate);


    Calendar cal = Calendar.getInstance();
     cal.setTimeZone(TimeZone.getTimeZone("GMT-1"));
     try {

      ContentResolver cr = this.getContentResolver();
      ContentValues values = new ContentValues();

      values.put(Events.DTSTART, calendarStart.getTimeInMillis());
      values.put(Events.DTEND, calendarEnd.getTimeInMillis());
      values.put(Events.TITLE, title);
      values.put(Events.DESCRIPTION, description);
      values.put(Events.CALENDAR_ID, 1 );
      values.put("rrule", "FREQ=DAILY");          // values.put(Events._ID, meeting_id);
      values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());

      Uri uri = cr.insert(Events.CONTENT_URI, values);
      eventID = Long.parseLong(uri.getLastPathSegment());
        Toast toast =Toast.makeText(getApplicationContext(), "Calender Event Added" + eventID,Toast.LENGTH_LONG);
        toast.show();

     } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
    }
于 2013-11-13T12:38:13.737 回答
0

我遇到了这个问题,然后我使用以下代码来获取日历 ID。

private fun getCalendarId(context: Context) : Long? {
    val projection = arrayOf(Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME)

    var calCursor = context.contentResolver.query(
        Calendars.CONTENT_URI,
        projection,
        Calendars.VISIBLE + " = 1 AND " + Calendars.IS_PRIMARY + "=1",
        null,
        Calendars._ID + " ASC"
    )

    if (calCursor != null && calCursor.count <= 0) {
        calCursor = context.contentResolver.query(
            Calendars.CONTENT_URI,
            projection,
            Calendars.VISIBLE + " = 1",
            null,
            Calendars._ID + " ASC"
        )
    }

    if (calCursor != null) {
        if (calCursor.moveToFirst()) {
            val calName: String
            val calID: String
            val nameCol = calCursor.getColumnIndex(projection[1])
            val idCol = calCursor.getColumnIndex(projection[0])

            calName = calCursor.getString(nameCol)
            calID = calCursor.getString(idCol)

            log("Calendar name = $calName Calendar ID = $calID")

            calCursor.close()
            return calID.toLong()
        }
    }
    return null
}

所以不要将 0、1 或 3 作为 Calendar ID 传递。请改用上述功能。

于 2019-07-26T06:27:04.823 回答