Create a calendar with Android 4.0 Content Provider (ICS)

I am trying to create a calendar using the new ICS calendaring content provider. Since creating a calendar can only be done in SyncAdapter, I created one of them:

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(); } } 

But it does not start inside the Service, as I see everywhere, because all I want to do is use it to insert a new Calendar. Running in the service is not required? It seems not.

In any case, I create an instance in my work and call my method:

 public void createCalendar( View v ) { m_syncAdapter.doCreateCalendar( this, m_account ); } 

Successful, no complaints, but the returned URI is always the same. When I parse it for Row ID, it's always 12. And when I check my calendars, there is nothing new.

In my manifest, I have all the settings:

 ... <application ... <activity android:name="Setup" android:label="@string/activity_title_setup"> <intent-filter> <action android:name="android.content.SyncAdapter" /> </intent-filter> <meta-data android:name="android.content.SyncAdapter" android:resource="@xml/calendar_sync_adapter" /> </activity> 

And calendar_sync_adapter.xml:

 <?xml version="1.0" encoding="utf-8"?> <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android" android:contentAuthority="com.android.calendar" android:accountType="com.google" android:userVisible="true" /> 

So I'm not sure what I'm doing wrong here. But this is what I see in the logs when I execute it:

 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) 
+4
source share
3 answers

Immediate response: Synchronization adapters must be connected to the Service. The service creates an instance of the synchronization adapter in onCreate () and returns an IBinder for the synchronization adapter in onBind (). SyncManager calls the service when it wants to start your synchronization adapter.

Sync adapters are controlled by Android, in particular SyncManager, and not by your application.

Sample SampleSyncAdapter shows how to write a synchronization adapter and a service. If you do not want to authenticate, you can delete this part. All you have to do is implement a class that extends AbstractThreadedSyncAdapter and its associated Service class along with the synchronization adapter XML files.

The best answer: Synchronization adapters help synchronize data between the server and the device. If all you want to do is download the calendar data from the server to your device, you can use an application that removes data from the server and writes it to the calendar provider. This is not synchronization; you make no guarantee that you will make more downloads or downloads. Of course, if you want to synchronize two data sources, the synchronization adapter has the following advantages:

  • The system will run it on a regular basis without user intervention.
  • The system ensures that the network is available before trying to start synchronization. Please note, however, that the system cannot stop you from doing dumb things, for example, trying to authenticate with the server when your network services are offline.
  • The system ensures that no other synchronization for the same server and content provider will be started when your synchronization starts.
  • The system provides a standard tool for displaying available synchronization adapters when a user wants to add an account.
+3
source

Yes, the calendar API is not suitable for creating new calendars. The device should be larger than a cloud mirror.

+2
source

It is not possible to create a new syncable calendar in an existing Google Account (Gmail) programmatically using the Android Calendar API. I will provide more details: fooobar.com/questions/351179 / ...

0
source

Source: https://habr.com/ru/post/1397114/


All Articles