我正在尝试使用ICS的新日历内容提供程序创建日历.由于日历创建只能在SyncAdapter中完成,我创建了一个:

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( Debug.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, 0xFFFFFFFF);

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( Debug.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();

}

}

但它并不像我到处看到的那样在服务中运行,因为我想要的只是使用它来插入新的日历.不需要在服务中运行吗?似乎不是.

无论如何,我在我的Activity中实例化它并调用我的方法:

public void createCalendar( View v )

{

m_syncAdapter.doCreateCalendar( this, m_account );

}

它成功执行,没有投诉,但它返回的URI始终是相同的.当我为行ID解析它时,它始终是12.当我查看我的日历时,没有什么新东西.

在我的清单中,我有一切设置:

...

...

而calendar_sync_adapter.xml:

android:contentAuthority="com.android.calendar"

android:accountType="com.google"

android:userVisible="true"

/>

所以不确定我在这里做错了什么.但这是我执行时在日志中看到的:

02-17 16:43:07.783: I/ActivityManager(184): Start proc com.android.providers.calendar for content provider com.android.providers.calendar/.CalendarProvider2: pid=20132 uid=10008 gids={3003, 1015}

02-17 16:43:07.806: I/ActivityThread(20132): Pub com.android.calendar: com.android.providers.calendar.CalendarProvider2

02-17 16:43:07.869: I/ActivityManager(184): Start proc com.google.android.calendar for service com.google.android.calendar/com.google.android.syncadapters.calendar.CalendarSyncAdapterService: pid=20145 uid=10007 gids={3003}

02-17 16:43:07.900: I/ActivityThread(20145): Pub com.google.android.calendar.CalendarRecentSuggestionsProvider: com.android.calendar.CalendarRecentSuggestionsProvider

// This is my print of the returned URI

02-17 16:43:08.009: I/C3(19995): Calendar created: 12 - content://com.android.calendar/calendars/12?caller_is_syncadapter=true&account_name=xxx%40gmail.com&account_type=com.google

02-17 16:43:08.876: I/CalendarProvider2(20132): Sending notification intent: Intent { act=android.intent.action.PROVIDER_CHANGED dat=content://com.android.calendar }

02-17 16:43:08.876: W/ContentResolver(20132): Failed to get type for: content://com.android.calendar (Unknown URL content://com.android.calendar)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐