17

我有以下代码用于将事件添加到日历。

问题是我不知道如何检索默认日历 ID。

long calID = 3;
long startMillis = 0; 
long endMillis = 0;     
Calendar beginTime = Calendar.getInstance();
beginTime.set(2013, 3, 23, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2013, 3, 24, 8, 45);
endMillis = endTime.getTimeInMillis();

ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, endMillis);
values.put(Events.TITLE, "My Test");
values.put(Events.DESCRIPTION, "My Calendar Test");
values.put(Events.CALENDAR_ID, calID);
values.put(Events.EVENT_TIMEZONE, "Israel/tel-aviv");
Uri uri = cr.insert(Events.CONTENT_URI, values);

行:long calID = 3;是日历ID

是否可以从 Android 获取默认日历 ID,或者我是否需要显示日历列表并让用户选择一个?

如果不可能,如何显示日历帐户列表?

4

5 回答 5

17

要获取日历列表,您需要像这样查询 ContentResolver:

public MyCalendar [] getCalendar(Context c) {

    String projection[] = {"_id", "calendar_displayName"};
    Uri calendars;
    calendars = Uri.parse("content://com.android.calendar/calendars");

    ContentResolver contentResolver = c.getContentResolver();
    Cursor managedCursor = contentResolver.query(calendars, projection, null, null, null);

    if (managedCursor.moveToFirst()){
        m_calendars = new MyCalendar[managedCursor.getCount()];
        String calName;
        String calID;
        int cont= 0;
        int nameCol = managedCursor.getColumnIndex(projection[1]);
        int idCol = managedCursor.getColumnIndex(projection[0]);
        do {
            calName = managedCursor.getString(nameCol);
            calID = managedCursor.getString(idCol);
            m_calendars[cont] = new MyCalendar(calName, calID);
            cont++;
        } while(managedCursor.moveToNext());
        managedCursor.close();
    }
    return m_calendars;

}
于 2013-05-20T17:07:21.787 回答
8

我使用以下代码获取日历 ID。

private fun getCalendarId(context: Context) : Long? {
    val projection = arrayOf(Calendars._ID, Calendars.CALENDAR_DISPLAY_NAME)

    var calCursor = context.contentResolver.query(
        Calendars.CONTENT_URI,
        projection,
        Calendars.VISIBLE + " = 1 AND " + Calendars.IS_PRIMARY + "=1",
        null,
        Calendars._ID + " ASC"
    )

    if (calCursor != null && calCursor.count <= 0) {
        calCursor = context.contentResolver.query(
            Calendars.CONTENT_URI,
            projection,
            Calendars.VISIBLE + " = 1",
            null,
            Calendars._ID + " ASC"
        )
    }

    if (calCursor != null) {
        if (calCursor.moveToFirst()) {
            val calName: String
            val calID: String
            val nameCol = calCursor.getColumnIndex(projection[1])
            val idCol = calCursor.getColumnIndex(projection[0])

            calName = calCursor.getString(nameCol)
            calID = calCursor.getString(idCol)

            Log.d("Calendar name = $calName Calendar ID = $calID")

            calCursor.close()
            return calID.toLong()
        }
    }
    return null
}

所以不要将 0、1 或 3 作为日历 ID。请改用上述功能。

此外,检查日历 ID 是否为 ,如果为null,则不要对其执行操作null

        val calendarId = getCalendarId(context)
        if (calendarId != null) {
        //Call operations e.g.: Insert operation
        }
于 2019-07-26T06:28:31.600 回答
7

中有一IS_PRIMARYCalendarContract.CalendarColumns。您选择查询:

CalendarContract.CalendarColumns.IS_PRIMARY + "=1"

但是,这是从 SDK 17 开始

于 2015-11-18T10:10:00.117 回答
4

一些最新版本有问题,有不同的可见日历列表,所以下面是选择主日历的代码,在旧设备中,此查询返回 0 记录,因此如果第一个返回 0 记录,则使用第二个。

Cursor calCursor = mContext.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1 AND "  + CalendarContract.Calendars.IS_PRIMARY + "=1", null, CalendarContract.Calendars._ID + " ASC");
if(calCursor.getCount() <= 0){
    calCursor = mContext.getContentResolver().query(CalendarContract.Calendars.CONTENT_URI, projection, CalendarContract.Calendars.VISIBLE + " = 1", null, CalendarContract.Calendars._ID + " ASC");
}
于 2017-01-02T09:48:22.500 回答
0

这段代码对我有用

private fun calendarId(): Long? {
    val uri = CalendarContract.Calendars.CONTENT_URI
    var cursor: Cursor?= null

    //Submit calendar query
    if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
        cursor = contentResolver?.query(uri, CALENDAR_PROJECTION, null, null, null)

    } else{
        ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.WRITE_CALENDAR), PERMISSION_REQUEST_WRITE_CALENDAR) }

     if (cursor != null) {
         Log.d("MainActivity", "Cursor is present")
         if(cursor.moveToFirst()) {
             //Get required fields
             val calID = cursor.getLong(CAL_ID)
             val calName = cursor.getLong(CAL_NAME)
             Log.d("MainActivity", "Calendar ID: $calID, Calendar Name: $calName")
             cursor.close()
             return calID
         }
     }

    //return null
    return null
}
于 2020-06-07T07:46:00.613 回答