我正在开发一个我希望与服务器同步数据的Android应用。为此,我使用Android sync api和SyncAdapter。现在,当我请求手动同步时,我的同步适配器中的onPerformSync方法被正确调用。我已添加此列表中的所有要点:https://stackoverflow.com/a/5255360/3795043
然而,当我尝试定时同步时,日志中有一个消息:
10-18 23:30:08.267 862-882/? W/SyncManager: Ignoring setPeriodicSyncTime request for a sync that does not exist. Authority: TwoRoomsSync/de.lehrbaum.tworooms.database:u0
为什么会这样?同步显然存在,否则手动请求它也不会工作,或?这是设置同步的代码:
final Account accountInstance = new Account(
account, account_type);
// Get an instance of the Android account manager
AccountManager accountManager =
(AccountManager) getSystemService(
ACCOUNT_SERVICE);
final boolean firstTime = accountManager.addAccountExplicitly(accountInstance, null, null);
if(firstTime) {
//set sync enabled by default
ContentResolver.setSyncAutomatically(accountInstance, authority, true);
ContentResolver.addPeriodicSync(accountInstance, authority,
Bundle.EMPTY, /* 5 hours interval */5 * 60 * 60);
//request a sync
final Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(
ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(accountInstance, authority, settingsBundle);
}
SyncAdapter.SYNC_OBSERVER = new ContentObserver(new Handler(getMainLooper())) {
@Override
public boolean deliverSelfNotifications() {
return false;
}
@Override
public void onChange(boolean selfChange) {
Log.i(TAG, "On change for sync called. self change: " + selfChange);
ContentResolver.requestSync(accountInstance, authority, Bundle.EMPTY);
}
};
final ContentResolver resolver = getContentResolver();
Uri uri = Uri.withAppendedPath(CONTENT_URI, VOTES_TABLE);
resolver.registerContentObserver(uri, true, SyncAdapter.SYNC_OBSERVER);
uri = Uri.withAppendedPath(CONTENT_URI, SETS_TABLE);
resolver.registerContentObserver(uri, true, SyncAdapter.SYNC_OBSERVER);
此外,当我在不添加“手动”标志的情况下请求同步时,它也不起作用。我不知道这两个问题是否相关。