本文介绍了Android 添加新日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经仔细检查了在 Android 应用中创建新日历的方法.我见过的唯一方法是在最新的 api 版本中使用新的日历 API,但这似乎只有在您使用 CalendarContract.ACCOUNT_TYPE_LOCAL 时才有效.它不会让您创建附加到用户 Google 帐户的日历,并与 Google 日历 同步.它不会出现在 Google 日历 的在线日历列表中.

I've checked all over for ways to create a new calendar from within an Android app. The only way I've seen is using the new Calendar API in the latest api version, BUT this only seems to work if you use CalendarContract.ACCOUNT_TYPE_LOCAL. It won't let you create a Calendar attached to the users Google account, synchronized with Google Calendar. It doesn't appear in the calendar list online at Google Calendar.

values.put(CalendarContract.Calendars.ACCOUNT_TYPE,
    CalendarContract.ACCOUNT_TYPE_LOCAL);

我已尝试使用帐户中的帐户类型,但这不起作用.虽然它似乎没有产生任何错误.结果恢复正常.

I've tried using the account type from the Account, but this does not work. It doesn't seem to produce any error though. The result comes back fine.

values.put(CalendarContract.Calendars.ACCOUNT_TYPE, accountType);

这是使用 Calendar APISYNCADAPTER(帐户类型注释掉)添加新本地日历的完整代码.有谁知道我正在尝试做的事情是否可行?

Here is the full code for adding a new local calendar using the Calendar API and SYNCADAPTER (account type commented out). Does anyone know if what I'm trying to do is even possible?

AccountManager manager = AccountManager.get(context);
Account[] accounts = manager.getAccountsByType("com.google");

String accountName = "";
String accountType = "";

for (Account account : accounts) {
    accountName = account.name;
    accountType = account.type;
    break;
}

ContentResolver cr = context.getContentResolver();
ContentValues values = new ContentValues();

values.put(CalendarContract.Calendars.ACCOUNT_NAME, accountName);
//values.put(CalendarContract.Calendars.ACCOUNT_TYPE, accountType);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, CalendarContract.ACCOUNT_TYPE_LOCAL);
values.put(CalendarContract.Calendars.NAME, DaysSince.CAL_NAME);
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, DaysSince.CAL_NAME);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountName);
values.put(CalendarContract.Calendars.DIRTY, 1);
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getDefault().getID());

Uri calUri = CalendarContract.Calendars.CONTENT_URI;

calUri = calUri.buildUpon()
        .appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
        .appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType)
        .build();

Uri result = cr.insert(calUri, values);

推荐答案

是的,你可以做到.但是由于您想创建一个与您的 google 帐户链接的日历,因此您需要 Google Calendar API(没有找到使用 Android Calendar Provider 执行此操作的单一解决方案).检查以下链接:

Yes you can do it. But since you want to create a calendar linked with your google account, you need Google Calendar API for this (didn't find a single solution for doing this with Android Calendar Provider). Check below links:

插入新的 Google 日历

请务必查看他们的 API 浏览器.用于创建新日历的简单 HTTP 请求:

Be sure to check their API explorer. Simple HTTP REQUEST for creating new calendar:

POST https://www.googleapis.com/calendar/v3/calendars?key={YOUR_API_KEY}

{
 "summary": "My new google calendar"
}

它是响应:

{
 "kind": "calendar#calendar",
 "etag": "__________etag_val_________",
 "id": "[email protected]",
 "summary": "My new google calendar"
}

之后,您应该可以使用日历提供程序从您的 Android 应用管理此日历(同步后).

After that you should be able to manage this calendar from your Android app with Calendar Provider (after sync).

这篇关于Android 添加新日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 17:05