我正在使用此代碼在我現有的帳戶中創建新的日曆。新Google日曆未使用同步適配器進行同步
CalendarSyncAdapter m_syncAdapter = new CalendarSyncAdapter(this, true);
Account acc = new Account("[email protected]", "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
,但我看不出它的網絡。請糾正我在哪裏錯了?謝謝!
在模擬器/設備中同步設置爲'ON' – Nezam
@Nezam m在設備中運行時,我在同步的設備中手動創建了一個日曆。謝謝! –
我應該發佈一個答案讓你接受嗎? – Nezam