如何查询特定日期范围的Android日历事件?

Chi*_*imu 8 events android calendar selection

我在android中查询事件表来获取事件.

String[] projection = new String[] { "calendar_id", "title", "description",
                    "dtstart", "dtend", "eventLocation" };

Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/events"),
                    projection,
                    null,
                    null,
                    null);
Run Code Online (Sandbox Code Playgroud)

这将返回所有事件.但是现在我只想在特定时间内举办活动,例如2013年4月4日至2013年3月3日.如何使用selectionselectionArgs[]?我试过这个

String selection = "((dtstart <= ?) AND (dtend >= ?))";
String[] selectionArgs = new String[] {startString, endString};
Run Code Online (Sandbox Code Playgroud)

但它没有返回任何东西.我怀疑是1. where where子句有用吗?因为,startDate和endDate首先转换为long(毫秒),然后转换为String,然后存储在表中.那么如何比较字符串的长值.toNumber()sqlite中没有任何功能.如果我selectionArgs当时通过startString,endString应该采用哪种格式?

Moh*_*lah 8

startString&endString应该以毫秒为单位传递.尝试使用:

Calendar c_start= Calendar.getInstance();
c_start.set(2013,2,4,0,0); //Note that months start from 0 (January)   
Calendar c_end= Calendar.getInstance();
c_end.set(2013,2,7,0,0); //Note that months start from 0 (January)
Run Code Online (Sandbox Code Playgroud)

另外,我认为你的逻辑是相反的,日期范围应该是这样的:

String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))";
Run Code Online (Sandbox Code Playgroud)