0

こんにちは私は calendar を処理することになっている 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 に答える