日历事件未显示在日历中

时间:2017-09-19 09:37:38

标签: android xamarin calendar

我有一个以xamarin形式创建的Android应用程序,我使用下面的代码添加日历事件。

enterContentValues eventValues = new ContentValues();
            eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId, eventId);
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Title, Title);
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Description, Description);
            eventValues.Put(CalendarContract.Events.InterfaceConsts.EventTimezone, "Local");
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtstart, GetDateTimeMS(StartDate.Year, StartDate.Month, StartDate.Day, StartDate.Hour, StartDate.Minute));
            eventValues.Put(CalendarContract.Events.InterfaceConsts.Dtend, GetDateTimeMS(EndDate.Year, EndDate.Month, EndDate.Day, EndDate.Hour, EndDate.Minute));
            var uri = Forms.Context.ContentResolver.Insert(CalendarContract.Events.ContentUri, eventValues);
            Console.WriteLine("Uri for new event: {0}", uri);
            long eventID = long.Parse(uri.LastPathSegment);
            return true; 

我在执行该功能时没有任何异常。我也得到了uri和eventID。但是在执行此操作后,我无法在日历中看到添加的事件。请帮帮我

2 个答案:

答案 0 :(得分:5)

我找到了答案......

这行代码, eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId,eventId);

我为每个事件添加到日历传递了不同的eventId ..即我正在创建一个事件的id并且传递了该id。现在我没有传递这个不同的东西,而是只传递了1作为参数。

即eventValues.Put(CalendarContract.Events.InterfaceConsts.CalendarId,1);

它奏效了!!!

答案 1 :(得分:1)

日历内容提供商不是Android SDK的一部分。它可能无法在某些已使用自己的应用程序替换默认日历应用程序的设备上运行。请查看此博客:Be Careful With Content Providers

如果您仔细检查了应用的权限和代码,但仍然找不到任何问题,那么如果您不介意,可以使用Intent插入日历事件,例如:

Intent calIntent = new Intent(Intent.ActionInsert);
calIntent.SetData(CalendarContract.Events.ContentUri);
calIntent.SetType("vnd.android.cursor.item/event");
calIntent.PutExtra(CalendarContract.Events.InterfaceConsts.Title, "Test Event from M4A");
calIntent.PutExtra(CalendarContract.Events.InterfaceConsts.Description, "This is an event created from Xamarin.Android");

long lDtStart = GetDateTimeMS(2017, 9, 20, 14, 0);
long lDtEnd = GetDateTimeMS(2017, 9, 20, 16, 0);

calIntent.PutExtra(CalendarContract.ExtraEventBeginTime, lDtStart);
calIntent.PutExtra(CalendarContract.ExtraEventEndTime, lDtEnd);

calIntent.PutExtra(CalendarContract.Events.InterfaceConsts.EventTimezone, "UTC");
calIntent.PutExtra(CalendarContract.Events.InterfaceConsts.EventEndTimezone, "UTC");

StartActivity(calIntent);

<强>更新

要在运行时请求这些权限,您可以使用以下代码:

private readonly string[] PermissionsCalendar =
{
    Manifest.Permission.ReadCalendar,
    Manifest.Permission.WriteCalendar
};

private const int RequestCalendarId = 0;

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    // Create your application here
    SetContentView(Resource.Layout.layout);

    ...

    if ((int)Build.VERSION.SdkInt >= 23)
    {
        var permissionread = Manifest.Permission.ReadCalendar;
        var permissionwrite = Manifest.Permission.WriteCalendar;
        if (CheckSelfPermission(permissionread) != (int)Permission.Granted || CheckSelfPermission(permissionwrite) != (int)P
        {
            RequestPermissions(PermissionsCalendar, RequestCalendarId);
        }
        else
        {
            //TODO:
        }
    }
    else
    {
        //TODO:
    }
    ...
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Permission[] grantResults)
{
    switch (requestCode)
    {
        case RequestCalendarId:
            {
                if (grantResults[0] == Permission.Granted)
                {
                    //Permission granted
                    //TODO:
                }
                else
                {
                    //Permission Denied :(
                    //TODO:
                }
            }
            break;
    }
}