我可以使用以下代码在设备的日历上设置事件:
Calendar cal = Calendar.getInstance();
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("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
现在它显示一个弹出屏幕,以便用户可以决定是否设置此事件,那么有没有办法自动生成?我的意思是我可以设置此事件或通过编程取消它,而不是等待用户的触摸
就像我设置闹钟的方式一样,在我的startActivity(意图)后,它被自动设置(不显示设置的弹出闹钟)。
答案 0 :(得分:2)
您可以使用此代码在日历上添加活动
try
{
Calendar cal = Calendar.getInstance();
String[] timedevide = scheduletime.split(":");
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH),
Integer.parseInt(timedevide[0]),
Integer.parseInt(timedevide[1]), 00);
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(ctx) + "events");
ContentResolver cr = ctx.getContentResolver();
// event insert
ContentValues values = new ContentValues();
values.put("calendar_id", 1);
values.put("title", title);
values.put("allDay", 0);
values.put("dtstart", cal.getTimeInMillis());
values.put("dtend", cal.getTimeInMillis() + 60 * 60 * 1000);
values.put("description", description);
values.put("visibility", 0);
values.put("hasAlarm", 1);
Uri event = cr.insert(EVENTS_URI, values);
addKeyToPreference(Long.parseLong(event.getLastPathSegment()));
// reminder insert
Uri REMINDERS_URI = Uri.parse(getCalendarUriBase(ctx) + "reminders");
values = new ContentValues();
values.put("event_id", Long.parseLong(event.getLastPathSegment()));
values.put("method", 1);
values.put("minutes", mTotalMinute);
cr.insert(REMINDERS_URI, values);
}
catch(Exception e)
{
e.printStackTrace();
}
private String getCalendarUriBase(Activity activity) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = activity.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = activity.managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
更新
现在不推荐使用托管查询
Android 4.0(API Level 14)添加了CalendarContract
ContentProvider。
希望这会有所帮助
答案 1 :(得分:1)
好消息和坏消息。
好消息:是的,使用CalendarContract
可以做到这一点
坏消息:这个api是在ICS(Android 4.0)中引入的,在此之前无法使用。