1

我正在使用此代码在我现有的帐户中创建一个新的日历。

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但在网络上看不到它。请纠正我我错在哪里?谢谢!

4

2 回答 2

0

尝试以这种方式强制启动同步操作:

 cr.requestSync();

在你成功地在手机上创建日历之后。给它某个时间。

于 2013-05-28T05:58:09.660 回答
0

不,不可能使用现有的 Android 日历 API 以编程方式在现有的 Google (Gmail) 帐户中创建新的可同步日历。我在这里放了更多细节:https ://stackoverflow.com/a/19734278/237232

于 2013-11-01T20:00:54.590 回答