Search code examples
androidandroid-syncadapter

SyncAdapter only syncs once


I have a big problem with my sync adapter. In a first step, I want to call sync everytime, a special activity is resumed (very inefficient, I know but its only for testing). When I call my ContentResolver.requestSync the first time after starting the app (installing from Android Studio, user data is available), it calls my onPerformSync method. But later when I call requestSync, the onPerformSyncmethod is never been called. This is the code I call every time to trigger a sync:

public void updateContent(Activity activity) {
    final Account account = APIHelper.getInstance().getAccount();
    final Bundle bundle = new Bundle();
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);


    Context context = MyApp.getApplication().getApplicationContext();
    final AccountManager manager = AccountManager.get(context);
    if (account == null) {
       manager.addAccount(Constants.ACCOUNT_TYPE, "", null, null, activity, new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> future) {
                try {
                    Bundle bnd = future.getResult();
                    final Account account1 = APIHelper.getInstance().getAccount();
                    ContentResolver.requestSync(account1, Constants.AUTH_PROVIDER_NAME, bundle);
                } catch (Exception e) {
                    Log.e(TAG, e.getLocalizedMessage());
                }
            }
        }, null);

    } else {
        ContentResolver.requestSync(account, Constants.AUTH_PROVIDER_NAME, bundle);

    }
}

When I check the Account settings in Android, it seemes that the sync is in Progress (the animated sync symbol is turning and it shows "sync in progress". I have also noticed this post, tried to add this in my code, but without an effect:

ContentResolver.setSyncAutomatically(account, getString(R.string.CONTENT_AUTHORITY), true);
ContentResolver.setIsSyncable(account, getString(R.string.CONTENT_AUTHORITY), 1);

I tried to modify it to a periodic sync which syncs every minute, but this also does not work.


Solution

  • I finally solved the problem: in my SyncAdapter, i tried to send a Toast to the UI. I did this via a handler. This seems to break the SyncAdapter. When I removed this logic, it works.