【发布时间】:2017-09-29 15:05:52
【问题描述】:
我在我的项目中使用了一个同步适配器,它会定期同步。要为同步适配器创建帐户,我使用以下代码。
我面临的问题是此代码正在触发初始同步。文档中没有提到此代码将使同步开始运行。
事实上,即使在 google 示例项目中,也有额外的代码用于触发我已删除的初始同步。
我使用了此示例中的代码: http://developer.android.com/samples/BasicSyncAdapter/index.html
即使我添加命令 ContentResolver.cancelSync(account, null);同步适配器仍在运行。
如何阻止同步适配器最初同步。它应该在同步间隔期过后第一次同步。
Account account = new Account(context.getPackageName(), context.getPackageName());
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
if (accountManager.addAccountExplicitly(account, null, null)) {
// Inform the system that this account supports sync
ContentResolver.setIsSyncable(account, context.getPackageName(), 1);
// Inform the system that this account is eligible for auto sync when the network is up
ContentResolver.setSyncAutomatically(account, context.getPackageName(), true);
// Recommend a schedule for automatic synchronization.
// The system may modify this based
// on other scheduled syncs and network utilization.
ContentResolver.addPeriodicSync(account, context.getPackageName(),
Bundle.EMPTY, AppConstants.SYNC_INTERVAL);
}
【问题讨论】:
标签: android android-contentresolver android-syncadapter