答案 0 :(得分:6)
将这些放在一个函数
中喜欢
public void calenderevent(Calendar begintime, Calendar endtime){
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", begintime.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", endtime.getTimeInMillis()+60*60*1000);
intent.putExtra("title", "A Test Event from android app");
startActivity(intent);
}
答案 1 :(得分:3)
从ICS开始,有一个更好的答案,正如博客中记载的那样,现在有一个官方的API。
Blog entry on the calendar APIs in ICS
这是developer.android.com上的documentation
干杯!
答案 2 :(得分:3)
这就是Android Build API 8到ICS 15的全部内容。
String[] calendarsProjection = {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME
};
String calName;
String calId = null;
Uri calendars= Uri.parse("content://com.android.calendar/events");
Cursor managedCursor = managedQuery(calendars, calendarsProjection, null, null, null);
if (managedCursor.moveToFirst())
{
int nameColumn = managedCursor.getColumnIndex("account_name");
int idColumn = managedCursor.getColumnIndex("_id");
do
{
calName = managedCursor.getString(nameColumn);
calId = managedCursor.getString(idColumn);
Log.e("Calendar Id : ",""+calId+" : "+calName);
}
while (managedCursor.moveToNext());
}
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 6, 18, 13, 10, 10);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 6, 18, 16, 10, 10);
endMillis = endTime.getTimeInMillis();
System.out.println("Date start :"+startMillis);
System.out.println("Date start :"+endMillis);
// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 1 );
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC");
Uri uri = cr.insert(Uri.parse("content://com.android.calendar/events"), values);
答案 3 :(得分:1)
String[] calendarsProjection = {
CalendarContract.Calendars._ID,
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME
};
String calName;
String calId = null;
Uri calendars= Uri.parse("content://com.android.calendar/events");
Cursor managedCursor = managedQuery(calendars, calendarsProjection, null, null, null);
if (managedCursor.moveToFirst())
{
int nameColumn = managedCursor.getColumnIndex("account_name");
int idColumn = managedCursor.getColumnIndex("_id");
do
{
calName = managedCursor.getString(nameColumn);
calId = managedCursor.getString(idColumn);
Log.e("Calendar Id : ",""+calId+" : "+calName);
}
while (managedCursor.moveToNext());
}
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 6, 18, 13, 10, 10);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 6, 18, 16, 10, 10);
endMillis = endTime.getTimeInMillis();
System.out.println("Date start :"+startMillis);
System.out.println("Date start :"+endMillis);
// Insert Event
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, startMillis);
values.put(CalendarContract.Events.DTEND, endMillis);
values.put(CalendarContract.Events.TITLE, "Walk The Dog");
values.put(CalendarContract.Events.DESCRIPTION, "My dog is bored, so we're going on a really long walk!");
values.put(CalendarContract.Events.CALENDAR_ID, 1 );
values.put(CalendarContract.Events.EVENT_TIMEZONE, "UTC");
Uri uri = cr.insert(Uri.parse("content://com.android.calendar/events"), values);
答案 4 :(得分:0)
嗯......最后两篇文章在ICS中运行良好但在其他帖子中没有。我建议这堂课from google code。