DontPanic
DontPanic

Reputation: 2406

Android Resolver Query for CalendarContract?

I am trying to write an app to dump Calendar entries for reference and backup. To get started, I tried to dump the column names of the CalendarContract using the code below but it always fails with an exception:

try {
    Cursor cur = Resolver.query(CalendarContract.CONTENT_URI, null, null, null, null);
    // print column names...

}
catch( Exception e ) {
    Log.i( "MYAPP", "Exception on Resolver.query: "+e);  // ALWAYS HAPPENS
    return;
}

The exception is "javal.lang.IllegalArgumentException: Unknown URL content://com.android.calendar".

Autocomplete shows "CalendarContract.CONTENT_URI" is a valid member.
I also tried dumping the Contacts column names using a similar query and this works just fine:

Cursor cur = Resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
// print column names  

Note: I am running on a Samsung S9 running Andoid V10.

What am I doing wrong?

Upvotes: 1

Views: 195

Answers (2)

DontPanic
DontPanic

Reputation: 2406

Based on @marmor's response, I was able to do my query like so...

Cursor cur = Resolver.query( CalendarContract.Events.CONTENT_URI );

int ncol = cur.getColumnCount();
for( int indx=0; indx<ncol; indx++ ) {
  String colname = cur.getColumnName( indx );
  // print indx, colname here
}

On my device the title was index +73 and the date/time (msec) was index +47. So

String title = cur.getString( 73 );
Long msec    = cur.getLong( 47 );  

I formatted 'msec' with 'SimpleDataFormat()' and print the data and title. For example:

12/31/2021  "New Year's Eve"
etc...

which was my goal.

Upvotes: 0

marmor
marmor

Reputation: 28179

This content_uri cannot be queried directly, you might want to use CalendarContract.Events.CONTENT_URI instead.

Read the docs here: https://developer.android.com/reference/android/provider/CalendarContract

and more specifically: https://developer.android.com/reference/android/provider/CalendarContract.Events

Upvotes: 1

Related Questions