Best practice for obtaining Calendar ID

MartHughMartHugh USMember ✭✭✭
edited February 12 in Xamarin.Android

When I write events to the Android Calendar I find the Calendar ID I need to use varies depending on the OS Version/installation.

  1. What is the best practise for determining the Calendar ID.

  2. When trying to read back Calendar providers I am getting a Security Exception (requires android.permission.READ_CALENDAR, or grantUriPermission())
    when I query CalendarContract.Calendars.ContentUri

    This is despite having

      uses-permission android:name="android.permission.READ_CALENDAR" 
      uses-permission android:name="android.permission.WRITE_CALENDAR" 
    

set in the Manifest

The code leading to the exception is as follows

        if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteCalendar) != (int)Permission.Granted)
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.WriteCalendar }, REQUESTWRITECALENDAR);
            if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadCalendar) != (int)Permission.Granted)
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.ReadCalendar }, REQUESTREADCALENDAR);

            ICursor cursor = null;
            ContentResolver contentResolver = context.ContentResolver;
            var calendars = CalendarContract.Calendars.ContentUri;

            String[] EVENT_PROJECTION = new String[] {
            CalendarContract.Calendars.InterfaceConsts.Id,                           // 0
            CalendarContract.Calendars.InterfaceConsts.AccountName,                  // 1
            CalendarContract.Calendars.InterfaceConsts.CalendarDisplayName,         // 2
            CalendarContract.Calendars.InterfaceConsts.OwnerAccount,                 // 3
            CalendarContract.Calendars.InterfaceConsts.IsPrimary                     // 4
        };

            int PROJECTION_ID_INDEX = 0;
            int PROJECTION_ACCOUNT_NAME_INDEX = 1;
            int PROJECTION_DISPLAY_NAME_INDEX = 2;
            int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
            int PROJECTION_VISIBLE = 4;

            cursor = contentResolver.Query(calendars, EVENT_PROJECTION, null, null, null);

Any suggestions ?
Thanks.

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo so that we can test with it?

  • MartHughMartHugh USMember ✭✭✭

    I managed to resolve this. It was a failure to programmatically set permissions because I was calling RequestPermissions for each permission, rather than calling it once with a list of permissions.

Sign In or Register to comment.