我正在开发一个Android应用程序,其中我想处理日历。我无法在自己的日历中添加活动。我使用了以下代码。使用该代码,我可以在默认日历中添加事件..请建议我在我的代码中添加事件所需的更改
private void addevent() {
// TODO Auto-generated method stub
Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(Events.TITLE, "My House Party");
calIntent.putExtra(Events.EVENT_LOCATION, "My Beach House");
calIntent.putExtra(Events.DESCRIPTION, "A Pig Roast on the Beach");
GregorianCalendar calDate = new GregorianCalendar(2013, 4, 16);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
calDate.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calDate.getTimeInMillis());
startActivity(calIntent);
Toast.makeText(this, "event added", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:3)
如果在日历中使用以下代码添加事件,则可以在我的日历中使用。
//此代码用于在日历中添加事件。
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.ALL_DAY, Events.ALL_DAY_VALUE);
values.put(Events.DTSTART, startTimeInMilli);
values.put(Events.DTEND, endTimeInMilli);
values.put(Events.TITLE, strTaskName);
values.put(Events.DESCRIPTION, strDescription);
values.put(Events.CALENDAR_ID, 0);
values.put(Events.VISIBILITY, Events.VISIBILITY_VALUE);
Uri EVENTS_URI = Uri.parse(getCalendarUriBase(this) + "events");
Uri uri = cr.insert(EVENTS_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
//这个函数调用高级代码。
private String getCalendarUriBase(Activity act) {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = act.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 = act.managedQuery(calendars, null, null, null,
null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}
//事件常量的其他静态类。
public class Events {
public static String ALL_DAY = "allDay";
public static String DTSTART = "dtstart";
public static String DTEND = "dtend";
public static String TITLE = "title";
public static String DESCRIPTION = "description";
public static String CALENDAR_ID = "calendar_id";
public static String EVENT_TIMEZONE = "eventTimezone";
public static String VISIBILITY = "visibility";
public static String HASALARM = "hasAlarm";
public static int VISIBILITY_VALUE = 0;
public static int HASALARM_VALUE = 1;
public static int ALL_DAY_VALUE = 0;
}
答案 1 :(得分:0)
您需要在意图putExtra中传递应用的日历ID 。
calIntent.putExtra(Events.CALENDAR_ID , my_cal_id);
要获取设备中所有可用日历的列表,您可以使用以下代码:
if(android.os.Build.VERSION.SDK_INT
>= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
cursor = getContentResolver().query(Calendars.CONTENT_URI,new String[]
{ Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME }, null ,null, null);
else
cursor = getContentResolver().query(Uri.parse("content://com.android.calendar
/calendars"),new String[] { "_id", "displayName" }, "selected=1",null, null);
if (cursor != null && cursor.moveToFirst()) {
String[] calNames = new String[cursor.getCount()];
int[] calIds = new int[cursor.getCount()];
for (int i = 0; i < calNames.length; i++) {
calIds[i] = cursor.getInt(0);
calNames[i] = cursor.getString(1);
System.out.println(""+cursor.getInt(0) + " -- "+
cursor.getString(1));
cursor.moveToNext();
}
cursor.close();