1

I need to have multiple SyncAdapter. I now have exactly seven of them to synchronize differnt data between App and Server.

To start my adapters i do the following:

public class SyncUtils {
    public static final String ACCOUNT_NAME = "My Account";
    public static final String ACCOUNT_TYPE = "de.my.package";
    public static final long SECONDS_PER_MINUTE = 60L;
    public static final long SYNC_INTERVAL_IN_MINUTES = 2L;
    public static final long SYNC_INTERVAL = SYNC_INTERVAL_IN_MINUTES * SECONDS_PER_MINUTE;
    private static boolean newAccount = false;

    public static void createSyncAccount(Context context, String email) {
        boolean setupComplete = PreferencesSync.getSetupComplete(context, email);

        AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);

        setUpSyncOne(accountManager);
        setUpSyncTwo(accountManager);
        setUpSyncThree(accountManager);
        setUpSyncFour(accountManager);
        setUpSyncFive(accountManager);
        setUpSyncSix(accountManager);
        setUpSyncSeven(accountManager);

        if (newAccount || !setupComplete) {
            syncRefreshImmediatly(ContentProviderOne.AUTHORITY);
            syncRefreshImmediatly(ContentProviderTwo.AUTHORITY);
            syncRefreshImmediatly(ContentProviderThree.AUTHORITY);
            syncRefreshImmediatly(ContentProviderFour.AUTHORITY);
            syncRefreshImmediatly(ContentProviderFive.AUTHORITY);
            syncRefreshImmediatly(ContentProviderSix.AUTHORITY);
            syncRefreshImmediatly(ContentProviderSeven.AUTHORITY);

            PreferencesSync.setSetupComplete(context, email, true);
        }
    }

    private static void setUpSyncOne(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderOne.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncTwo(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderTwo.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncThree(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderThree.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncFour(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderFour.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncFive(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderFive.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncSix(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderSix.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    private static void setUpSyncSeven(AccountManager accountManager) {
        Account account = getAccount();

        if (accountManager.addAccountExplicitly(account, null, null)) {
            ContentResolver.addPeriodicSync(account, ContentProviderSeven.AUTHORITY, new Bundle(), SYNC_INTERVAL);

            newAccount = true;
        }
    }

    public static void syncRefresh(String contentAuthority, Bundle bundle) {
        ContentResolver.requestSync(getAccount(), contentAuthority, bundle);
    }

    public static void syncRefreshImmediatly(String contentAuthority) {
        Bundle bundle = new Bundle();

        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);

        syncRefresh(contentAuthority, bundle);
    }

    public static void syncRefreshGCM(String contentAuthority, String gcm, long addition) {
        Bundle bundle = new Bundle();

        bundle.putString(Globals.KEY_GCM, gcm);
        bundle.putLong(Globals.KEY_ADDITION, addition);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);

        syncRefresh(contentAuthority, bundle);
    }

    public static Account getAccount() {
        return new Account(ACCOUNT_NAME, ACCOUNT_TYPE);
    }
}

When i now install my app the account manager of my android device shows me the account for my app together with all seven syncadapters. My problem now is, that there displayed names are all the same:

My App

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

My App
Touch to start synchronization
Synchronization: Disabled

But i actually want it to be:

My App 

SycAdapter One
Touch to start synchronization
Synchronization: Disabled

SycAdapter Two
Touch to start synchronization
Synchronization: Disabled

SycAdapter Thre
Touch to start synchronization
Synchronization: Disabled

SycAdapter Four
Touch to start synchronization
Synchronization: Disabled

SycAdapter Five
Touch to start synchronization
Synchronization: Disabled

SycAdapter Six
Touch to start synchronization
Synchronization: Disabled

SycAdapter Seven
Touch to start synchronization
Synchronization: Disabled

How can i do that?

EDIT

The SyncAdapters shown in the Account Manager are all correct! It means when i click on the first entry with the label My App my SyncAdapter One gest started. The second entry with the label My App starts my SyncAdapter Two and so on. So the Adapter are all there correctly they just have the same Label. So how can i change the labels?

Mulgard
  • 9,877
  • 34
  • 129
  • 232

2 Answers2

1

For each SyncAdapter you need one AuthenticatorService with authenticator.xml and different account names and types

UMESH0492
  • 1,701
  • 18
  • 31
  • i did that. that was even worse. as i did that the account manager showed 7 entries of my app and each entry contained all seven sync adapters oO – Mulgard Mar 16 '16 at 20:00
  • it will show 7 entries, but each would contain one, there is some configuration mismatch. Account type was also different ? – UMESH0492 Mar 16 '16 at 20:02
  • and checkout authorities configuration also. will solve it. – UMESH0492 Mar 16 '16 at 20:05
  • i have different authorities for each adapter. i have the same account type for each adapter. i now have different account anmes for each adapter. when i now go into account manager there is written: accounts: SyncAdapter One, SyncAdapter Two, etc. And if i click for example on SyncAdapter One it navigates to the exact same view i described in my openingpost: My App, Touch to synchronize, Synchronization disabled. – Mulgard Mar 16 '16 at 20:07
  • try to have different account type for segmentation from one another. – UMESH0492 Mar 16 '16 at 20:09
  • Try to different description to every adapter, for testing are they same or not – UMESH0492 Mar 16 '16 at 20:10
  • but if i have different account type for each syncadapter it would mean to also create one authenticator.xml for each syncadapter. is that correct? – Mulgard Mar 16 '16 at 20:11
  • I have a ContentProvider for each SyncAdapter and every ContentProvider has its own Authority. – Mulgard Mar 16 '16 at 20:16
  • 1
    oh ok. now i understand. i thought it is possible to have some kind of parent entry in the account manager which contains all 7 syncadapters. but in fact i have 7 entries in the account manager. one for each syncadapter. ok. to have one "parent" with 7 children is not possible is it? If not, thank you very much. Maybe add to your answer that for each SyncAdapter you need one AuthenticatorService with authenticator.xml and different account names and types because i could not find this information anywhere in the web. – Mulgard Mar 16 '16 at 20:31
  • It's not possible to have one "parent" with 7 children. Its again the concept. – UMESH0492 Mar 16 '16 at 20:48
  • hello, its me again. it has to be possible: http://stackoverflow.com/questions/30998063/android-multiple-sync-adapter-items-like-google-account – Mulgard Mar 16 '16 at 21:43
1

Got it! The answer was hidden:

    <provider
        android:name="de.hof.ime_dc.idia_move.provider.ContentProviderMeasure"
        android:authorities="de.hof.ime_dc.idia_move.measures.contentprovider"
        android:exported="false"
        android:label="@string/provider_measures"
        android:syncable="true" />

I had to set the label of my providers! I did not define them so android took my app name automatically!

Mulgard
  • 9,877
  • 34
  • 129
  • 232