以编程方式创建的日历未保存
Progrommatically created Calendar not saved
我正在尝试使用 this topic
中的代码创建日历
private static void createCalendar( Context mContext, Account account)
{
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.NAME,"TEST");
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "TEST");
v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
// v.put(CalendarContract.Calendars._ID, 123);
v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
v.put(CalendarContract.Calendars.VISIBLE, 1);
Uri creationUri = asSyncAdapter(CalendarContract.Calendars.CONTENT_URI, account.name, account.type);
Uri calendarData = mContext.getContentResolver().insert(creationUri, v);
long id = Long.parseLong(calendarData.getLastPathSegment());
}
private static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter
(CalendarContract.Calendars.ACCOUNT_NAME,account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType) .build();
}
然后拨打电话:
createCalendar(c, AccountManager.get(c).getAccountsByType("com.google")[0]); // i've checked - it's correct account to add calendar
然后我从 ContentResolver
中检索数据
final ContentResolver cr = ctx.getContentResolver();
Cursor cursor ;
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"),null, null, null, null);
并在光标中查看新日历。但是:
它没有出现在google帐户中
它会在一段时间后删除(猜测,因为同步)
在 5.1.1 上测试
如何以编程方式为我的帐户创建日历?
尝试使用 calendar.insert 创建另一个日历。
由于您使用的是 Java,我认为这段代码可能会有所帮助:
import com.google.api.services.calendar.Calendar;
// ...
// Initialize Calendar service with valid OAuth credentials
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName("applicationName").build();
// Create a new calendar
com.google.api.services.calendar.model.Calendar calendar = new Calendar();
calendar.setSummary("calendarSummary");
calendar.setTimeZone("America/Los_Angeles");
// Insert the new calendar
Calendar createdCalendar = service.calendars().insert(calendar).execute();
System.out.println(createdCalendar.getId());
给 Try-it 一个干燥的 运行 看看它是否有效。
我正在尝试使用 this topic
中的代码创建日历 private static void createCalendar( Context mContext, Account account)
{
final ContentValues v = new ContentValues();
v.put(CalendarContract.Calendars.NAME,"TEST");
v.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, "TEST");
v.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
v.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
v.put(CalendarContract.Calendars.CALENDAR_COLOR, Color.GREEN);
v.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_OWNER);
v.put(CalendarContract.Calendars.OWNER_ACCOUNT, account.name);
// v.put(CalendarContract.Calendars._ID, 123);
v.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
v.put(CalendarContract.Calendars.VISIBLE, 1);
Uri creationUri = asSyncAdapter(CalendarContract.Calendars.CONTENT_URI, account.name, account.type);
Uri calendarData = mContext.getContentResolver().insert(creationUri, v);
long id = Long.parseLong(calendarData.getLastPathSegment());
}
private static Uri asSyncAdapter(Uri uri, String account, String accountType)
{
return uri.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true").appendQueryParameter
(CalendarContract.Calendars.ACCOUNT_NAME,account)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, accountType) .build();
}
然后拨打电话:
createCalendar(c, AccountManager.get(c).getAccountsByType("com.google")[0]); // i've checked - it's correct account to add calendar
然后我从 ContentResolver
final ContentResolver cr = ctx.getContentResolver();
Cursor cursor ;
cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"),null, null, null, null);
并在光标中查看新日历。但是:
它没有出现在google帐户中
它会在一段时间后删除(猜测,因为同步)
在 5.1.1 上测试
如何以编程方式为我的帐户创建日历?
尝试使用 calendar.insert 创建另一个日历。
由于您使用的是 Java,我认为这段代码可能会有所帮助:
import com.google.api.services.calendar.Calendar;
// ...
// Initialize Calendar service with valid OAuth credentials
Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credentials)
.setApplicationName("applicationName").build();
// Create a new calendar
com.google.api.services.calendar.model.Calendar calendar = new Calendar();
calendar.setSummary("calendarSummary");
calendar.setTimeZone("America/Los_Angeles");
// Insert the new calendar
Calendar createdCalendar = service.calendars().insert(calendar).execute();
System.out.println(createdCalendar.getId());
给 Try-it 一个干燥的 运行 看看它是否有效。