新的Google日历未使用同步适配器同步

时间:2013-05-28 04:32:03

标签: android android-calendar

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

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的设备中看到新的日历,但我无法在网络上看到它。请纠正我在哪里错了?谢谢!

2 个答案:

答案 0 :(得分:0)

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

 cr.requestSync();

在手机上成功创建日历后。有时候给它。

答案 1 :(得分:0)

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