SyncRequest.Builder
public static class SyncRequest.Builder
extends Object
@link SyncRequest的生成器类。 在构建SyncRequest时,此类还将执行验证。
Summary
Public constructors
SyncRequest.Builder
SyncRequest.Builder ()
Public methods
build
SyncRequest build ()
如果此验证失败,则执行对请求的验证并引发运行时异常 IllegalArgumentException
。
Returns |
SyncRequest |
a SyncRequest with the information contained within this builder. |
setDisallowMetered
SyncRequest.Builder setDisallowMetered (boolean disallow)
如果被调用并且 setIgnoreSettings(boolean)
已经被调用,将抛出 IllegalArgumentException
。
Parameters |
disallow |
boolean : true to allow this transfer on metered networks. Default false. |
setExpedited
SyncRequest.Builder setExpedited (boolean expedited)
加速同步立即运行,并可以抢占其他非加速运行同步。 对于周期性同步无效, IllegalArgumentException
在build()
抛出build()
。
Parameters |
expedited |
boolean : whether to run expedited. Default false. |
setExtras
SyncRequest.Builder setExtras (Bundle bundle)
实际发生同步时,开发人员提供的附加功能会返回。 该捆绑包被复制到由build()返回的build()
。 例:
String[] syncItems = {"dog", "cat", "frog", "child"};
SyncRequest.Builder builder =
new SyncRequest.Builder()
.setSyncAdapter(dummyAccount, dummyProvider)
.syncOnce();
for (String syncData : syncItems) {
Bundle extras = new Bundle();
extras.setString("data", syncData);
builder.setExtras(extras);
ContentResolver.sync(builder.build()); // Each sync() request creates a unique sync.
}
Only values of the following types may be used in the extras bundle:
- Integer
- Long
- Boolean
- Float
- Double
- String
- Account
- null
If any data is present in the bundle not of this type, build() will throw a runtime exception.
Parameters |
bundle |
Bundle : extras bundle to set. |
setNoRetry
SyncRequest.Builder setNoRetry (boolean noRetry)
方便功能设置SYNC_EXTRAS_DO_NOT_RETRY
。 除非设置为false,否则失败的一次性同步操作将以指数回退重试。 对于定期同步无效,并会在build()中引发IllegalArgumentException
。
Parameters |
noRetry |
boolean : true to not retry a failed sync. Default false. |
setRequiresCharging
SyncRequest.Builder setRequiresCharging (boolean requiresCharging)
指定同步是否需要插入电话。
Parameters |
requiresCharging |
boolean : true if sync requires the phone to be plugged in. Default false. |
setSyncAdapter
SyncRequest.Builder setSyncAdapter (Account account,
String authority)
为此传输指定一个权限和帐户。
Parameters |
account |
Account : Account to sync. Can be null unless this is a periodic sync, for which verification by the ContentResolver will fail. If a sync is performed without an account, the |
authority |
String : A String identifying the content provider to be synced. |
syncOnce
SyncRequest.Builder syncOnce ()
请求立即发生同步。 例
SyncRequest.Builder builder = (new SyncRequest.Builder()).syncOnce();
syncPeriodic
SyncRequest.Builder syncPeriodic (long pollFrequency,
long beforeSeconds)
建立一个定期同步。 必须为此构建器调用this或syncOnce()。 同步由目标android.provider
和附加软件包的内容标识。 在指定周期性同步后(通过调用此函数),您不能重复使用同一个构建器进行一次性同步。 如果你这样做, IllegalArgumentException
将被抛出。
用于定期同步的软件包可以使用getPeriodicSyncs(Account, String)
由具有正确权限的应用程序查询,因此不应在此传输敏感数据。 用法示例。
Request a periodic sync every 5 hours with 20 minutes of flex.
SyncRequest.Builder builder =
(new SyncRequest.Builder()).syncPeriodic(5 * HOUR_IN_SECS, 20 * MIN_IN_SECS);
Schedule a periodic sync every hour at any point in time during that hour.
SyncRequest.Builder builder =
(new SyncRequest.Builder()).syncPeriodic(1 * HOUR_IN_SECS, 1 * HOUR_IN_SECS);
N.B.: Periodic syncs are not allowed to have any of
SYNC_EXTRAS_DO_NOT_RETRY
,
SYNC_EXTRAS_IGNORE_BACKOFF
,
SYNC_EXTRAS_IGNORE_SETTINGS
,
SYNC_EXTRAS_INITIALIZE
,
SYNC_EXTRAS_FORCE
,
SYNC_EXTRAS_EXPEDITED
,
SYNC_EXTRAS_MANUAL
set to true. If any are supplied then an
IllegalArgumentException
will be thrown.
Parameters |
pollFrequency |
long : the amount of time in seconds that you wish to elapse between periodic syncs. A minimum period of 1 hour is enforced. |
beforeSeconds |
long : the amount of flex time in seconds before pollFrequency that you permit for the sync to take place. Must be less than pollFrequency and greater than MAX(5% of pollFrequency , 5 minutes) |