0

嗨,我正在开发一个我想在其中处理日历的 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();

    }
4

2 回答 2

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;

}
于 2013-04-15T10:25:06.260 回答
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();
于 2013-04-15T10:26:17.793 回答