[英]Sync Adapter called twice using ContentObserver
So I have been having this problem where onPerformSync is called first when I explicitly call requestSync and secondly after 30 or 60 seconds but mostly 60 seconds, its weird. 因此,我一直遇到这样的问题:在我显式调用requestSync时首先调用onPerformSync,然后在30或60秒(但主要是60秒)之后调用它,这很奇怪。 I am using a ContentObserver and this behaviour is only happening when I use a ContentObserver. 我正在使用ContentObserver,并且仅当我使用ContentObserver时才会发生此行为。 I tried calling requestSync directly from my content provider and no additional onPerformSync were triggered. 我尝试直接从我的内容提供者调用requestSync,并且没有触发其他onPerformSync。 My code excerpts are listed below. 我的代码摘录如下。
Provider calls notifyChange 提供者调用notifyChange
@Nullable
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
Log.d(LOG_TAG, "Inserting with uri " + uri + " with values " + values.toString());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int match = sUriMatcher.match(uri);
....
// This direct requestSync call does not trigger extra syncs
//Bundle extras = new Bundle();
//extras.putString(SyncAdapter.CHANGED_URI, uri.toString());
//getContext().getContentResolver().requestSync(SyncAccount.getAccount(), Contract.CONTENT_AUTHORITY, extras);
getContext().getContentResolver().notifyChange(contentUri, null);
return contentUri;
}
ContentObserver with onChange 具有onChange的ContentObserver
public class TableObserver extends ContentObserver {
private final static String LOG_TAG = TableObserver.class.getSimpleName();
private final Account mAccount;
public TableObserver(Handler handler, Account account) {
super(handler);
mAccount = account;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.d(LOG_TAG, "Provider changed here: " + uri.toString());
Bundle args = new Bundle();
args.putString(SyncAdapter.CHANGED_URI, uri.toString());
ContentResolver.requestSync(mAccount, Contract.CONTENT_AUTHORITY, args);
}
}
Sync adapter with onPerformSync 带有onPerformSync的同步适配器
public class SyncAdapter extends AbstractThreadedSyncAdapter {
private static final String LOG_TAG = SyncAdapter.class.getSimpleName();
public static final String CHANGED_URI = "changed_uri";
private ContentResolver mContentResolver;
public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
mContentResolver = context.getContentResolver();
}
public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
mContentResolver = context.getContentResolver();
}
// This is automatically performed in a background thread.
@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.d(LOG_TAG, "Synchronizing: " + extras.get(CHANGED_URI));
}
}
通过将false传递给notifyChange来修复它,该方法确实删除了该方法与网络行为的同步,如下所示:
getContext().getContentResolver().notifyChange(contentUri, null, false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.