Android 日历在 10 分钟后删除日历和事件
Android calendar deletes calendar and events after 10 minutes
我一直在开发一款涉及制作活动的应用程序。我已经在 Android 的默认日历上成功创建了一个日历,并且我已经能够在此日历上添加、删除或更新事件。但是,我注意到我创建的日历会在大约 10 分钟后自动删除。这可能是 S 计划程序(默认日历)或 Google 日历的问题吗?这就是我制作日历的方式:
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, Utils.APP_PACKAGE).build();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars._ID, MY_CALENDAR_ID);
values.put(CalendarContract.Calendars.ACCOUNT_NAME, accountName);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, Utils.APP_PACKAGE);
values.put(CalendarContract.Calendars.NAME, "TBDAppName (" + accountName + ")");
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, Utils.APP_PACKAGE);
values.put(CalendarContract.Calendars.CALENDAR_COLOR, 0x00FF00);
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountName);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
context.getContentResolver().insert(target, values);
这就是我插入事件的方式:
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2018, 01, 01, 7, 30); //random time to test inserting.
startMillis = beginTime.getTimeInMillis();
ContentValues values = new ContentValues();
values.put(Events.CALENDAR_ID, MY_CALENDAR_ID);
values.put(Events._ID, MY_EVENT_ID);
values.put(Events.TITLE, MY_EVENT_TITLE);
values.put(Events.DESCRIPTION, "test description");
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, startMillis);
values.put(Events.CALENDAR_ID, MY_CALENDAR_ID);
TimeZone timeZone = TimeZone.getDefault();
values.put(Events.EVENT_TIMEZONE, timeZone.getID());
context.getContentResolver().insert(Events.CONTENT_URI, values);
创建日历时,必须将其设置为ACCOUNT_TYPE_LOCAL
,否则会被删除。这在 http://developer.android.com/reference/android/provider/CalendarContract.html 中描述如下;
public static final String ACCOUNT_TYPE_LOCAL
A special account type for calendars not
associated with any account. Normally calendars that do not match an
account on the device will be removed. Setting the account_type on a
calendar to this will prevent it from being wiped if it does not match
an existing account.
我一直在开发一款涉及制作活动的应用程序。我已经在 Android 的默认日历上成功创建了一个日历,并且我已经能够在此日历上添加、删除或更新事件。但是,我注意到我创建的日历会在大约 10 分钟后自动删除。这可能是 S 计划程序(默认日历)或 Google 日历的问题吗?这就是我制作日历的方式:
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, Utils.APP_PACKAGE).build();
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars._ID, MY_CALENDAR_ID);
values.put(CalendarContract.Calendars.ACCOUNT_NAME, accountName);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, Utils.APP_PACKAGE);
values.put(CalendarContract.Calendars.NAME, "TBDAppName (" + accountName + ")");
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, Utils.APP_PACKAGE);
values.put(CalendarContract.Calendars.CALENDAR_COLOR, 0x00FF00);
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, accountName);
values.put(CalendarContract.Calendars.VISIBLE, 1);
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
context.getContentResolver().insert(target, values);
这就是我插入事件的方式:
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2018, 01, 01, 7, 30); //random time to test inserting.
startMillis = beginTime.getTimeInMillis();
ContentValues values = new ContentValues();
values.put(Events.CALENDAR_ID, MY_CALENDAR_ID);
values.put(Events._ID, MY_EVENT_ID);
values.put(Events.TITLE, MY_EVENT_TITLE);
values.put(Events.DESCRIPTION, "test description");
values.put(Events.DTSTART, startMillis);
values.put(Events.DTEND, startMillis);
values.put(Events.CALENDAR_ID, MY_CALENDAR_ID);
TimeZone timeZone = TimeZone.getDefault();
values.put(Events.EVENT_TIMEZONE, timeZone.getID());
context.getContentResolver().insert(Events.CONTENT_URI, values);
创建日历时,必须将其设置为ACCOUNT_TYPE_LOCAL
,否则会被删除。这在 http://developer.android.com/reference/android/provider/CalendarContract.html 中描述如下;
public static final String ACCOUNT_TYPE_LOCAL
A special account type for calendars not associated with any account. Normally calendars that do not match an account on the device will be removed. Setting the account_type on a calendar to this will prevent it from being wiped if it does not match an existing account.