我想使用以下代码将事件添加到我的原生日历中。它工作正常。
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
final Calendar cal = Calendar.getInstance();
try {
cal.setTime(formatter.parse(datetime));
eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
Log.e("event date",eventdate);
}
catch (ParseException e) {
e.printStackTrace();
}
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", title);
intent.putExtra("description",desc);
c.startActivity(intent);
这会打开一个屏幕,要求我保存事件。当我点击保存时,它会将我的活动保存到日历。下面是截图。
现在,我想要的是将活动直接添加到原生日历,而不显示上面的屏幕。有没有办法做到这一点?
答案 0 :(得分:4)
您可以参考以下代码在日历中添加事件。
String GLOBAL_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
String gameDate="2015-07-12 01:10:00";
Date startDate = DateConstants.getDateFromString(
gameDate,GLOBAL_DATE_FORMAT);
long endDate = startDate.getTime()
+ (PTGConstantMethod.validateInteger(game
.getGame_duration()) * 1000);
ContentValues event = new ContentValues();
event.put("calendar_id", 1);
event.put("title", "Game#" + game.getId());
event.put("description", game.getLocation());
event.put("eventLocation", game.getLocation());
event.put("eventTimezone", TimeZone.getDefault().getID());
event.put("dtstart", startDate.getTime());
event.put("dtend", endDate);
event.put("allDay", 0); // 0 for false, 1 for true
event.put("eventStatus", 1);
event.put("hasAlarm", 1); // 0 for false, 1 for true
String eventUriString = "content://com.android.calendar/events";
Uri eventUri = context.getApplicationContext()
.getContentResolver()
.insert(Uri.parse(eventUriString), event);
long eventID = Long.parseLong(eventUri.getLastPathSegment());
// if reminder need to set
int minutes=120;
// add reminder for the event
ContentValues reminders = new ContentValues();
reminders.put("event_id", eventID);
reminders.put("method", "1");
reminders.put("minutes", minutes);
String reminderUriString = "content://com.android.calendar/reminders";
context.getApplicationContext().getContentResolver()
.insert(Uri.parse(reminderUriString), reminders);
答案 1 :(得分:2)
c.startActivity(intent);
不要使用此功能,因为这里通过调用此 try
{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm");
Calendar cal = Calendar.getInstance();
cal.setTime(formatter.parse(datetime));
eventdate = cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH)+" "+cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
Log.e("event date",eventdate);
// provide CalendarContract.Calendars.CONTENT_URI to
// ContentResolver to query calendars
Cursor cur = this.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI,null, null, null, null);
if (cur.moveToFirst())
{
long calendarID=cur.getLong(cur.getColumnIndex(CalendarContract.Calendars._ID));
ContentValues eventValues = new ContentValues();
// provide the required fields for creating an event to
// ContentValues instance and insert event using
// ContentResolver
eventValues.put (CalendarContract.Events.CALENDAR_ID, calendarID);
eventValues.put(CalendarContract.Events.TITLE, "Event 1"+title);
eventValues.put(CalendarContract.Events.DESCRIPTION," Calendar API"+desc);
eventValues.put(CalendarContract.Events.ALL_DAY,true);
eventValues.put(CalendarContract.Events.DTSTART, (cal.getTimeInMillis()+60*60*1000));
eventValues.put(CalendarContract.Events.DTEND, cal.getTimeInMillis()+60*60*1000);
eventValues.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
Uri eventUri = this.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, eventValues);
eventID = ContentUris.parseId(eventUri);
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (cur != null)
{
cur.close();
}
}
你正在开始一个活动,这将使用户在日历中并要求保存它而不是使用它你可以使用ContentResolver在你的信息中插入事件
n喜欢这个
pname
在保存事件按钮中编写此代码,它将以您正在查看的方式工作