阅读两个日期之间的日历事件

huangapple go评论82阅读模式
英文:

Read calendar events between 2 dates

问题

我正尝试在两个日期之间读取日历事件。

我使用了谷歌开发者的示例(https://developer.android.com/guide/topics/providers/calendar-provider),但它不起作用。

当然,我在请求的日期有事件。

如果我读取日历的所有事件,它是有效的,所以一个(不好的)解决方案是对每个返回的事件进行检查,看它是否包含在间隔内...

以下是我的代码:

final String[] EVENT_PROJECTION = new String[]{
    CalendarContract.Events._ID,
    CalendarContract.Events.TITLE,
    CalendarContract.Events.DTSTART,
    CalendarContract.Events.DTEND,
};

final int PROJECTION_ID_INDEX = 0;
final int PROJECTION_TITLE_INDEX = 1;
final int PROJECTION_DTSTART_INDEX = 2;
final int PROJECTION_DTEND_INDEX = 3;

Calendar beginTime = Calendar.getInstance();
beginTime.set(2020, 3, 18, 0, 0);
long startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2020, 3, 24, 0, 0);
long endMillis = endTime.getTimeInMillis();

Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, startMillis);
ContentUris.appendId(builder, endMillis);

String selection = CalendarContract.Instances.CALENDAR_ID + " = " + calID;

if (ActivityCompat.checkSelfPermission(this, permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(builder.build(), EVENT_PROJECTION, selection, null, null);
    if (cursor != null) {
        while (cursor.moveToNext()) {
            Log.i (TAG, "ID=" + cursor.getLong(PROJECTION_ID_INDEX) + " Title=" + cursor.getString(PROJECTION_TITLE_INDEX));
        }
        cursor.close();
    }
}

构建器已经正确初始化(类似于这样:"content://com.android.calendar/instances/when/1584486000000/1584918000000"

以下代码能够正确地返回日历的所有事件:

Cursor eventCursor = cr.query(CalendarContract.Events.CONTENT_URI, EVENT_PROJECTION, selection, null, null);

我已经尝试了许多方法,但仍然没有结果...

有人知道为什么会失败吗?

谢谢

英文:

I'm trying to read calendar events between 2 dates.

I use the Google developer example (https://developer.android.com/guide/topics/providers/calendar-provider), but it's not working.

Of course, I have events at the requested dates.

If I read all events for the calendar it's working, so a (bad) solution would be to check for each returned event if it's included in the interval...

Here is my code :

    final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
};
final int PROJECTION_ID_INDEX = 0;
final int PROJECTION_TITLE_INDEX = 1;
final int PROJECTION_DTSTART_INDEX = 2;
final int PROJECTION_DTEND_INDEX = 3;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2020, 3, 18, 0, 0);
long startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2020, 3, 24, 0, 0);
long endMillis = endTime.getTimeInMillis();
Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, startMillis);
ContentUris.appendId(builder, endMillis);
String selection = CalendarContract.Instances.CALENDAR_ID + " = " + calID;
if (ActivityCompat.checkSelfPermission(this, permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(builder.build(), EVENT_PROJECTION, selection, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
Log.i (TAG, "ID=" + cursor.getLong(PROJECTION_ID_INDEX) + " Title=" + cursor.getString(PROJECTION_TITLE_INDEX));
}
cursor.close();
}
}

The builder is correctly initialized (something like that : "content://com.android.calendar/instances/when/1584486000000/1584918000000")

The code below is returning fine all events for the calendar :

Cursor eventCursor = cr.query(CalendarContract.Events.CONTENT_URI, EVENT_PROJECTION, selection, null, null);

I have tried many ways but still no results...

Someone knows why it fails ?

Thanks

答案1

得分: 0

这是解决方案,不完全是来自Google的代码...

final String[] EVENT_PROJECTION = new String[]{
    CalendarContract.Events._ID,
    CalendarContract.Events.TITLE,
    CalendarContract.Events.DTSTART,
    CalendarContract.Events.DTEND,
};

final int PROJECTION_ID_INDEX = 0;
final int PROJECTION_TITLE_INDEX = 1;
final int PROJECTION_DTSTART_INDEX = 2;
final int PROJECTION_DTEND_INDEX = 3;

Calendar beginTime = Calendar.getInstance();
beginTime.set(2020, 3, 18, 0, 0);
long startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2020, 3, 24, 0, 0);
long endMillis = endTime.getTimeInMillis();

String where = "((dtstart >= " + startMillis + ") AND (dtstart <= " + endMillis + ") AND (calendar_id = " + calID + "))";

if (ActivityCompat.checkSelfPermission(this, permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED) {

    ContentResolver cr = getContentResolver();
    Cursor cursor = cr.query(Events.CONTENT_URI, projection, where, null, null);
    if (cursor != null) {
        while (cursor.moveToNext()) {
            Log.i (TAG, "ID=" + cursor.getLong(PROJECTION_ID_INDEX) + " Title=" + cursor.getString(PROJECTION_TITLE_INDEX));
        }
        cursor.close();
    }

}
英文:

Here is the solution, not exactly the code from Google...

final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.TITLE,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
};
final int PROJECTION_ID_INDEX = 0;
final int PROJECTION_TITLE_INDEX = 1;
final int PROJECTION_DTSTART_INDEX = 2;
final int PROJECTION_DTEND_INDEX = 3;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2020, 3, 18, 0, 0);
long startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2020, 3, 24, 0, 0);
long endMillis = endTime.getTimeInMillis();
String where = &quot;((dtstart &gt;= &quot; + startMillis + &quot;) AND (dtstart &lt;= &quot; + endMillis + &quot;) AND (calendar_id = &quot; + calID + &quot;))&quot;;
if (ActivityCompat.checkSelfPermission(this, permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Events.CONTENT_URI, projection, where, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
Log.i (TAG, &quot;ID=&quot; + cursor.getLong(PROJECTION_ID_INDEX) + &quot; Title=&quot; + cursor.getString(PROJECTION_TITLE_INDEX));
}
cursor.close();
}
}

huangapple
  • 本文由 发表于 2020年4月4日 15:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61024922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定