我需要在默认的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版本中使用相同的东西。我应该更新代码? 感谢..
答案 0 :(得分: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();
}
}
答案 1 :(得分: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作为日历ID 。请改用上述功能。