我有以下代码将事件添加到日历。
问题在于我不知道如何检索默认日历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,或者我是否需要显示日历列表并让用户选择一个?
如果不可能,如何显示日历帐户列表?
答案 0 :(得分:14)
要获取日历列表,您需要像这样查询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;
}
答案 1 :(得分:5)
CalendarContract.CalendarColumns
中有一个IS_PRIMARY
列。您使用选择查询:
CalendarContract.CalendarColumns.IS_PRIMARY + "=1"
然而,这是自SDK 17以来的
答案 2 :(得分:4)
一些最新版本有问题,有不同的可见日历列表,所以下面是选择PRIMARY日历的代码,在旧设备中此查询返回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");
}
答案 3 :(得分:0)
我使用以下代码获取日历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。请改用上述功能。
答案 4 :(得分: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
}