Android帐户管理器中具有不同名称的多个SyncAdapter

时间:2016-03-16 19:54:33

标签: android android-syncadapter

我需要多个SyncAdapter。我现在正好有七个用于在App和Server之间同步不同的数据。

要启动我的适配器,请执行以下操作:

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

当我现在安装我的应用程序时,我的Android设备的客户经理会向我显示我的应用程序的帐户以及所有七个syncadapter。我现在的问题是,显示的名称都是相同的:

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

但实际上我希望它是:

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

我该怎么做?

修改

客户经理中显示的SyncAdapter都是正确的!这意味着当我点击标签为My App的第一个条目时,我的SyncAdapter One张开始了。带有标签My App的第二个条目将启动我的SyncAdapter Two,依此类推。所以适配器都正确,他们只有相同的标签。那么我该如何更改标签呢?

2 个答案:

答案 0 :(得分:1)

对于每个SyncAdapter,您需要一个带有authenticator.xml的AuthenticatorService以及不同的帐户名和类型

答案 1 :(得分:1)

知道了!答案被隐藏了:

    <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" />

我必须设置我的提供商的标签!我没有定义它们,所以android自动取了我的应用程序名称!