First time sync loops indefinitely

为君一笑 提交于 2020-01-01 05:35:27

问题


I'm having a situation with SyncAdapter I don't know how to fix.

I'm using periodic syncs. The onPerformSync method just logs some info for me to know that the process is working (no calls to notifyChanges in content providers or anything else).

The project consists of two apps: The first one creates a user account (for testing purposes only). The second holds the sync adapter. Note that this is perfectly legal for the scope of the project.

I first install the app with the account. I can see the account has been created.

Then I install the app with the sync adapter and the first time it runs the synchronization hangs. Seeing the account sync settings, the spinner icon is continuously running and no log messages are registered (meaning it does not reach onPerformSync).

However, I can cancel the sync in the Settings and then the sync process starts working normally. This means the wiring between Account, Content Provider and SyncService is properly set.

I'm aware that adding/removing an account triggers other sync processes so I let a good lapse of time to go before installing the app with the sync adapter.

Any hints on why this is happening?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mAccountManager = AccountManager.get(this);
    // No worries here. The account exists and it's the one I want

    Account[] accounts = mAccountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
    // Just first account for TESTING purposes
    if (accounts != null && accounts.length > 0)
        account = accounts[0];
    else {
        Log.e(TAG, "No accounts set!!");
        return;
    }

    // Set sync for this account.
    Bundle extras = new Bundle();
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);

    ContentResolver.setIsSyncable(account, authority, 1); // Mandatory since 3.1

    // Allows the provider to sync when internet connection is back
    ContentResolver.setSyncAutomatically(account, authority, true);


    // Add a periodic synchronization
    ContentResolver.addPeriodicSync(account, authority, extras, POLL_FREQUENCY);
}

EDIT

I found out that calling cancel on the sync, makes it work. Not the best solution but it fixes the problem by now. I put this line combined with a "isFirstUse" flag.

ContentResolver.cancelSync(account, authority); 

来源:https://stackoverflow.com/questions/17191838/first-time-sync-loops-indefinitely

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!