Hello,
I try to get Android calendar list so I searched and found a code wich several people use.
@SimpleFunction(description = "??")
public String loadCalendar(){
ContentResolver contentResolver = this.context.getContentResolver();
test+="contentResolver : "+contentResolver+"\n\n";
// Fetch a list of all calendars synced with the device, their display names and whether the
// user has them selected for display.
Cursor cursor = contentResolver.query(Uri.parse("content://calendar/calendars"),
(new String[] { "_id", "displayName", "selected","color" }), null, null, null);
// For a full list of available columns see http://tinyurl.com/yfbg76w
test+="cursor : "+cursor;
the problem is i don't get calendar list.
contentResolver contains :"android.app.ContextImp$ApplicationContentResolver@1c0e164"
but cursor contains :"null"
What version of Android are you on? I think newer versions may require you first ask the user for permission to access their calendars first otherwise the call will silently fail.
thank you for your answer ewpatton.
you'ra right i have to give calendar permissions.
but i found the real problem: text variables to acces calendar informations are wrong (even if we can see them lot of time on internet... )
here is the working code:
@SimpleFunction(description = "get Calendar list")
public List<String> CalendarList(){
List items = new ArrayList();
ContentResolver contentResolver = context.getApplicationContext().getContentResolver();
// Fetch a list of all calendars synced with the device, their display names and whether the
// user has them selected for display.
Cursor cursor = contentResolver.query(Uri.parse("content://com.android.calendar/calendars"),
(new String[] { "_id" , "calendar_displayName","calendar_color","isPrimary","visible"}), null, null, null);
// For a full list of available columns see http://tinyurl.com/yfbg76w
//ArrayList<PersonnalCalendar> calendarList = new ArrayList<PersonnalCalendar>();
while (cursor.moveToNext()) {
//Dictionary cal = new Hashtable();
Map<String, String> cal = new HashMap<String,String>();
cal.put("id", cursor.getString(0));
cal.put("displayName", cursor.getString(1));
cal.put("color", cursor.getString(2));
cal.put("isPrimary", cursor.getString(3));
cal.put("visible", Boolean.toString(!cursor.getString(4).equals("0")));
items.add(cal);
}
cursor.close();
return items;
}