public abstract class AbstractThreadedSyncAdapter
extends Object
java.lang.Object | |
↳ | android.content.AbstractThreadedSyncAdapter |
一个SyncAdapter的抽象实现,它产生一个线程来调用同步操作。 如果在收到同步请求时同步操作已在进行中,则会向新请求返回错误,并允许现有请求继续。 但是,如果没有同步正在进行,则会生成一个线程并在该线程上调用onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)
。
框架可以随时取消Syncs。 例如,一个非用户启动并且持续时间超过30分钟的同步将被视为超时并取消。 同样,框架将尝试通过在一分钟内监控其网络活动来确定适配器是否正在取得进展。 如果通过此窗口的网络流量足够接近零,则同步将被取消。 您还可以通过cancelSync(Account, String)
或cancelSync(SyncRequest)
请求取消同步。
通过在同步线程上发出interrupt()
来取消同步。 onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)
的代码必须检查interrupted()
,或者您必须覆盖onSyncCanceled(Thread)
onSyncCanceled()
(具体取决于您的适配器是否支持并行同步多个帐户)。 如果您的适配器不尊重框架发布的取消请求,那么您的应用程序的整个流程将被终止。
为了同步适配器必须扩展这个类,为抽象方法的实现,并编写返回结果的服务getSyncAdapterBinder()
在服务的onBind(android.content.Intent)
时与动作的通知调用android.content.SyncAdapter
。 此服务必须在其AndroidManifest.xml文件中指定以下意图过滤器和元数据标记
<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 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. Constants |
|
---|---|
int |
LOG_SYNC_DETAILS 此常数在API级别9中已弃用。私有常量。 可能会在下一个版本中消失。 |
Public constructors |
|
---|---|
AbstractThreadedSyncAdapter(Context context, boolean autoInitialize) 创建一个 |
|
AbstractThreadedSyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) 创建一个 |
Public methods |
|
---|---|
Context |
getContext() |
final IBinder |
getSyncAdapterBinder() |
abstract void |
onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) 为此帐户执行同步。 |
void |
onSecurityException(Account account, Bundle extras, String authority, SyncResult syncResult) 报告在打电话给 |
void |
onSyncCanceled(Thread thread) 表示同步操作已被取消。 |
void |
onSyncCanceled() 表示同步操作已被取消。 |
Inherited methods |
|
---|---|
From class java.lang.Object
|
int LOG_SYNC_DETAILS
此常数在API级别9中已弃用。
私有常量。 可能会在下一个版本中消失。
内核事件日志标记。 也列在数据/ etc / event-log-tags中。
常量值:2743(0x00000ab7)
AbstractThreadedSyncAdapter (Context context, boolean autoInitialize)
创建一个 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 (Context context, boolean autoInitialize, boolean allowParallelSyncs)
创建一个 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. |
IBinder getSyncAdapterBinder ()
Returns | |
---|---|
IBinder |
a reference to the IBinder of the SyncAdapter service. |
void onPerformSync (Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
为此帐户执行同步。 SyncAdapter特定的参数可以用extras来指定,它保证不为空。 这种方法的调用保证被序列化。
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 |
void onSecurityException (Account account, Bundle extras, String authority, SyncResult syncResult)
报告在打电话给onPerformSync(Account, Bundle, String, ContentProviderClient, SyncResult)
之前打开内容提供商时出现安全异常。 这将被视为同步数据库失败。
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 |
void onSyncCanceled (Thread thread)
表示同步操作已被取消。 这将在与同步线程不同的线程上调用,因此您必须考虑您在此方法中执行的工作的多线程含义。
这只会在SyncAdapter指出它支持并行同步时才会被调用。
Parameters | |
---|---|
thread |
Thread : the Thread of the sync that is to be canceled. |
void onSyncCanceled ()
表示同步操作已被取消。 这将在与同步线程不同的线程上调用,因此您必须考虑您在此方法中执行的工作的多线程含义。
这只会在SyncAdapter指出它不支持并行同步时才会被调用。