Most visited

Recently visited

Added in API level 5

AbstractThreadedSyncAdapter

public abstract class AbstractThreadedSyncAdapter
extends Object

java.lang.Object
   ↳ android.content.AbstractThreadedSyncAdapter


An abstract implementation of a SyncAdapter that spawns a thread to invoke a sync operation. If a sync operation is already in progress when a sync request is received, an error will be returned to the new request and the existing request will be allowed to continue. However if there is no sync in progress then a thread will be spawned and onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult) will be invoked on that thread.

Syncs can be cancelled at any time by the framework. For example a sync that was not user-initiated and lasts longer than 30 minutes will be considered timed-out and cancelled. Similarly the framework will attempt to determine whether or not an adapter is making progress by monitoring its network activity over the course of a minute. If the network traffic over this window is close enough to zero the sync will be cancelled. You can also request the sync be cancelled via cancelSync(Account, String) or cancelSync(SyncRequest).

A sync is cancelled by issuing a interrupt() on the syncing thread. Either your code in onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult) must check interrupted(), or you you must override one of onSyncCanceled(Thread)/onSyncCanceled() (depending on whether or not your adapter supports syncing of multiple accounts in parallel). If your adapter does not respect the cancel issued by the framework you run the risk of your app's entire process being killed.

In order to be a sync adapter one must extend this class, provide implementations for the abstract methods and write a service that returns the result of getSyncAdapterBinder() in the service's onBind(android.content.Intent) when invoked with an intent with action android.content.SyncAdapter. This service must specify the following intent filter and metadata tags in its AndroidManifest.xml file

   <intent-filter>
     <action android:name="android.content.SyncAdapter" />
   </intent-filter>
   <meta-data android:name="android.content.SyncAdapter"
             android:resource="@xml/syncadapter" />
 
The android:resource attribute must point to a resource that looks like:
 <sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="authority"
    android:accountType="accountType"
    android:userVisible="true|false"
    android:supportsUploading="true|false"
    android:allowParallelSyncs="true|false"
    android:isAlwaysSyncable="true|false"
    android:syncAdapterSettingsAction="ACTION_OF_SETTINGS_ACTIVITY"
 />
 

Summary

Constants

int LOG_SYNC_DETAILS

This constant was deprecated in API level 9. Private constant. May go away in the next release.

Public constructors

AbstractThreadedSyncAdapter(Context context, boolean autoInitialize)

Creates an AbstractThreadedSyncAdapter.

AbstractThreadedSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs)

Creates an AbstractThreadedSyncAdapter.

Public methods

Context getContext()
final IBinder getSyncAdapterBinder()
abstract void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)

Perform a sync for this account.

void onSecurityException(Account account, Bundle extras, String authority, SyncResult syncResult)

Report that there was a security exception when opening the content provider prior to calling onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult).

void onSyncCanceled(Thread thread)

Indicates that a sync operation has been canceled.

void onSyncCanceled()

Indicates that a sync operation has been canceled.

Inherited methods

From class java.lang.Object

Constants

LOG_SYNC_DETAILS

Added in API level 5
int LOG_SYNC_DETAILS

This constant was deprecated in API level 9.
Private constant. May go away in the next release.

Kernel event log tag. Also listed in data/etc/event-log-tags.

Constant Value: 2743 (0x00000ab7)

Public constructors

AbstractThreadedSyncAdapter

Added in API level 5
AbstractThreadedSyncAdapter (Context context, 
                boolean autoInitialize)

Creates an AbstractThreadedSyncAdapter.

Parameters
context Context: the Context that this is running within.
autoInitialize boolean: if true then sync requests that have SYNC_EXTRAS_INITIALIZE set will be internally handled by AbstractThreadedSyncAdapter by calling setIsSyncable(android.accounts.Account, String, int) with 1 if it is currently set to <0.

AbstractThreadedSyncAdapter

Added in API level 11
AbstractThreadedSyncAdapter (Context context, 
                boolean autoInitialize, 
                boolean allowParallelSyncs)

Creates an AbstractThreadedSyncAdapter.

Parameters
context Context: the Context that this is running within.
autoInitialize boolean: if true then sync requests that have SYNC_EXTRAS_INITIALIZE set will be internally handled by AbstractThreadedSyncAdapter by calling setIsSyncable(android.accounts.Account, String, int) with 1 if it is currently set to <0.
allowParallelSyncs boolean: if true then allow syncs for different accounts to run at the same time, each in their own thread. This must be consistent with the setting in the SyncAdapter's configuration file.

Public methods

getContext

Added in API level 5
Context getContext ()

Returns
Context

getSyncAdapterBinder

Added in API level 5
IBinder getSyncAdapterBinder ()

Returns
IBinder a reference to the IBinder of the SyncAdapter service.

onPerformSync

Added in API level 5
void onPerformSync (Account account, 
                Bundle extras, 
                String authority, 
                ContentProviderClient provider, 
                SyncResult syncResult)

Perform a sync for this account. SyncAdapter-specific parameters may be specified in extras, which is guaranteed to not be null. Invocations of this method are guaranteed to be serialized.

Parameters
account Account: the account that should be synced
extras Bundle: SyncAdapter-specific parameters
authority String: the authority of this sync request
provider ContentProviderClient: a ContentProviderClient that points to the ContentProvider for this authority
syncResult SyncResult: SyncAdapter-specific parameters

onSecurityException

Added in API level 23
void onSecurityException (Account account, 
                Bundle extras, 
                String authority, 
                SyncResult syncResult)

Report that there was a security exception when opening the content provider prior to calling onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult). This will be treated as a sync database failure.

Parameters
account Account: the account that attempted to sync
extras Bundle: SyncAdapter-specific parameters
authority String: the authority of the failed sync request
syncResult SyncResult: SyncAdapter-specific parameters

onSyncCanceled

Added in API level 11
void onSyncCanceled (Thread thread)

Indicates that a sync operation has been canceled. This will be invoked on a separate thread than the sync thread and so you must consider the multi-threaded implications of the work that you do in this method.

This will only be invoked when the SyncAdapter indicates that it does support parallel syncs.

Parameters
thread Thread: the Thread of the sync that is to be canceled.

onSyncCanceled

Added in API level 8
void onSyncCanceled ()

Indicates that a sync operation has been canceled. This will be invoked on a separate thread than the sync thread and so you must consider the multi-threaded implications of the work that you do in this method.

This will only be invoked when the SyncAdapter indicates that it doesn't support parallel syncs.

Hooray!