After factory reset the device. I am trying to get the names of the calendars displayed (by the code below), it returns that there are no calendars. but when you open the device’s Calendar application at least once, the default calendar will be restored correctly.
Is there a way to get calendars (especially the default) without opening the device’s Calendar app?
Thanks in advance.
Here is the code for getting calendars on the device:
private Uri getCalendarUri() { return Uri.parse(Integer.parseInt(Build.VERSION.SDK) > 7 ? "content://com.android.calendar/calendars" : "content://calendar/calendars"); } private String[] getCalendars(Context context) { String[] res = null; ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; try { cursor = contentResolver.query( getCalendarUri(), Integer.parseInt(Build.VERSION.SDK) > 13 ? new String[]{"_id", "calendar_displayName"} : new String[]{"_id", "displayName"}, null, null, "_id ASC"); if (cursor.getCount() > 0) { res = new String[cursor.getCount()]; int i = 0; while (cursor.moveToNext()) { res[i] = cursor.getString(0) + ": " + cursor.getString(1); i++; } } } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null) cursor.close(); } return res; }
source share