在Android Xamarin中使用我的日历的权限

时间:2015-07-23 16:26:42

标签: android xamarin calendar xamarin.forms

我正在尝试在我在Xamarin中制作的Android应用程序中向我的日历添加一个事件。但我不断收到错误“需要android.permission.READ_CALENDAR或android.permission.WRITE_CALENDAR”。有人可以帮我这个吗?这是我的代码..... 谢谢!!!

    using System;
    using MyApp;
    using MyApp.Droid;

    using Android.Content;
    using Android.Provider;
    using Xamarin.Forms;


    [assembly: Xamarin.Forms.Dependency (typeof (CalendarManager))]

    namespace MyApp.Droid
    {
        public class CalendarManager : ICalendarManager
        {
            public CalendarManager () {}

            //public const string WRITE_CALENDAR = "android.permission.WRITE_CALENDAR";
            //public const string READ_CALENDAR = "android.permission.READ_CALENDAR";


            //android.permission.WRITE_CALENDAR

            public void SaveEvent(DateTime start, DateTime end, string title, string notes, string location, bool allday) 
            {

                ContentValues eventDetails = new ContentValues();

                eventDetails.Put (CalendarContract.Events.InterfaceConsts.Dtstart, start.ToUniversalTime ().ToString ()); 
                eventDetails.Put (CalendarContract.Events.InterfaceConsts.Dtend, end.ToUniversalTime ().ToString ()); 
                eventDetails.Put(CalendarContract.Events.InterfaceConsts.Title, title);
                eventDetails.Put(CalendarContract.Events.InterfaceConsts.EventLocation, location);
                eventDetails.Put(CalendarContract.Events.InterfaceConsts.AllDay, allday);


            eventDetails.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");
            eventDetails.Put(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");

            var ctx = Forms.Context;
            ctx.ContentResolver.Insert(CalendarContract.Events.ContentUri, eventDetails);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

AndroidManifest.xml中,您必须声明应用所需的权限,以便Android在安装应用时通知用户。

设置“读取日历”和“写入日历”权限可以解决您的问题。

答案 1 :(得分:0)

您可以在应用程序&#34; AndroidManifest.xml&#34;中明确添加日历API的权限标记。 <manifest>标记内的文件:

<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />