我正在使用此代码在我现有的帐户中创建一个新的日历。
CalendarSyncAdapter m_syncAdapter = new CalendarSyncAdapter(this, true);
Account acc = new Account("my_gmail_acc@gmail.com", "www.google.com");
m_syncAdapter.doCreateCalendar(acc);
CalendarSyncAdapter 类
public class CalendarSyncAdapter extends AbstractThreadedSyncAdapter {
public CalendarSyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
}
@Override
public void onPerformSync(Account account, Bundle extras,
String authority, ContentProviderClient provider,
SyncResult syncResult) {
Log.i("log_tag", "onPerformSync()");
}
public void doCreateCalendar(Account account) {
ContentResolver cr = getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put(Calendars.ACCOUNT_NAME, account.name);
values.put(Calendars.ACCOUNT_TYPE, account.type);
values.put(Calendars.NAME, "newesttest");
values.put(Calendars.CALENDAR_DISPLAY_NAME, "newestTest");
values.put(Calendars.CALENDAR_COLOR, Color.GREEN);
values.put(Calendars.CALENDAR_ACCESS_LEVEL,
Calendars.CAL_ACCESS_OWNER);
values.put(Calendars.OWNER_ACCOUNT, account.name);
values.put(Calendars.SYNC_EVENTS, 1);
values.put(Calendars.VISIBLE, 1);
Uri creationUri = asSyncAdapter(Calendars.CONTENT_URI,
account.name, account.type);
Uri created = cr.insert(creationUri, values);
long id = Long.parseLong(created.getLastPathSegment());
Log.i("log_tag", "Calendar created: " + id + " - " + created);
}
private Uri asSyncAdapter(Uri uri, String account, String accountType) {
return uri
.buildUpon()
.appendQueryParameter(
CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType)
.build();
}
}
执行此代码后,我可以在设备中看到带有名称的新日历,newestTest
但在网络上看不到它。请纠正我我错在哪里?谢谢!