gpt4 book ai didi

java - 使用 ContentObserver 调用两次同步适配器

转载 作者:行者123 更新时间:2023-11-29 04:46:16 25 4
gpt4 key购买 nike

所以我一直遇到这个问题,当我显式调用 requestSync 时首先调用 onPerformSync,然后在 30 或 60 秒(但主要是 60 秒)后调用,这很奇怪。我使用的是 ContentObserver,此行为仅在我使用 ContentObserver 时发生。我尝试直接从我的内容提供商调用 requestSync,但没有触发额外的 onPerformSync。下面列出了我的代码摘录。

提供者调用 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;
}

带有 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);
}
}

带 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);

关于java - 使用 ContentObserver 调用两次同步适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36991529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com