How to get access to the calendars on a Android phone?

后端 未结 1 939
温柔的废话
温柔的废话 2020-12-07 06:30

I gave some tries to access the calendars on a Android phone, but none of my code worked, is there someone that knows for sure, how to read/query the calend

1条回答
  •  抹茶落季
    2020-12-07 07:19

    String[] projection = new String[] { CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION };
    
    // 0 = January, 1 = February, ...
    
    Calendar startTime = Calendar.getInstance();
    startTime.set(2014,00,01,00,00);
    
    Calendar endTime= Calendar.getInstance();
    endTime.set(2015,00,01,00,00);
    
    // the range is all data from 2014
    
    String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";
    
    Cursor cursor = this.getBaseContext().getContentResolver().query( CalendarContract.Events.CONTENT_URI, projection, selection, null, null );
    
    // output the events 
    
    if (cursor.moveToFirst()) {
        do {
            Toast.makeText( this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG ).show();
        } while ( cursor.moveToNext());
    }
    

    0 讨论(0)
提交回复
热议问题