public abstract class AbstractThreadedSyncAdapter extends Object
onPerformSync(Account, android.os.Bundle, java.lang.String, android.content.ContentProviderClient, android.content.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 ContentResolver.cancelSync(Account, String)
or
ContentResolver.cancelSync(SyncRequest)
.
A sync is cancelled by issuing a Thread.interrupt()
on the syncing thread. Either
your code in onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)
must check Thread.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 Service.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" />
android:contentAuthority
and android:accountType
attributes
indicate which content authority and for which account types this sync adapter serves.
android:userVisible
defaults to true and controls whether or not this sync
adapter shows up in the Sync Settings screen.
android:supportsUploading
defaults
to true and if true an upload-only sync will be requested for all syncadapters associated
with an authority whenever that authority's content provider does a
ContentResolver.notifyChange(android.net.Uri, android.database.ContentObserver, boolean)
with syncToNetwork set to true.
android:allowParallelSyncs
defaults to false and if true indicates that
the sync adapter can handle syncs for multiple accounts at the same time. Otherwise
the SyncManager will wait until the sync adapter is not in use before requesting that
it sync an account's data.
android:isAlwaysSyncable
defaults to false and if true tells the SyncManager
to intialize the isSyncable state to 1 for that sync adapter for each account that is added.
android:syncAdapterSettingsAction
defaults to null and if supplied it
specifies an Intent action of an activity that can be used to adjust the sync adapter's
sync settings. The activity must live in the same package as the sync adapter.
Modifier and Type | Field and Description |
---|---|
static int |
LOG_SYNC_DETAILS
Deprecated.
Private constant. May go away in the next release.
|
Constructor and Description |
---|
AbstractThreadedSyncAdapter(Context context,
boolean autoInitialize)
Creates an
AbstractThreadedSyncAdapter . |
AbstractThreadedSyncAdapter(Context context,
boolean autoInitialize,
boolean allowParallelSyncs)
Creates an
AbstractThreadedSyncAdapter . |
Modifier and Type | Method and Description |
---|---|
Context |
getContext() |
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, android.os.Bundle, java.lang.String, android.content.ContentProviderClient, android.content.SyncResult) . |
void |
onSyncCanceled()
Indicates that a sync operation has been canceled.
|
void |
onSyncCanceled(Thread thread)
Indicates that a sync operation has been canceled.
|
@Deprecated public static final int LOG_SYNC_DETAILS
public AbstractThreadedSyncAdapter(Context context, boolean autoInitialize)
AbstractThreadedSyncAdapter
.context
- the Context
that this is running within.autoInitialize
- if true then sync requests that have
ContentResolver.SYNC_EXTRAS_INITIALIZE
set will be internally handled by
AbstractThreadedSyncAdapter
by calling
ContentResolver#setIsSyncable(android.accounts.Account, String, int)
with 1 if it
is currently set to <0.public AbstractThreadedSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs)
AbstractThreadedSyncAdapter
.context
- the Context
that this is running within.autoInitialize
- if true then sync requests that have
ContentResolver.SYNC_EXTRAS_INITIALIZE
set will be internally handled by
AbstractThreadedSyncAdapter
by calling
ContentResolver#setIsSyncable(android.accounts.Account, String, int)
with 1 if it
is currently set to <0.allowParallelSyncs
- 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 Context getContext()
public final IBinder getSyncAdapterBinder()
public abstract void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
account
- the account that should be syncedextras
- SyncAdapter-specific parametersauthority
- the authority of this sync requestprovider
- a ContentProviderClient that points to the ContentProvider for this
authoritysyncResult
- SyncAdapter-specific parameterspublic void onSecurityException(Account account, Bundle extras, String authority, SyncResult syncResult)
onPerformSync(Account, android.os.Bundle, java.lang.String, android.content.ContentProviderClient, android.content.SyncResult)
. This will be treated as a sync
database failure.account
- the account that attempted to syncextras
- SyncAdapter-specific parametersauthority
- the authority of the failed sync requestsyncResult
- SyncAdapter-specific parameterspublic void onSyncCanceled()
This will only be invoked when the SyncAdapter indicates that it doesn't support parallel syncs.
public void onSyncCanceled(Thread thread)
This will only be invoked when the SyncAdapter indicates that it does support parallel syncs.
thread
- the Thread of the sync that is to be canceled.