Custom sync not working with Google Account (com.g

2019-06-18 02:59发布

问题:

I implemented a sync task the same way as the BasicSyncAdapter example except with a Google account like in this answer:

https://stackoverflow.com/a/2972918/2072569

It works on allmost all devices except for the Samsung SM-P600 (Galaxy Note 2014) with Android 4.4.2 and some other Samsung tablets.

My ContentProvider in the Manifest file has a label. This is the cause of this bug according to this post at some Android version of some Samsung tablets.

Has Samsung blocked adding sync tasks to a Google account for some reason?

The sync is added like this:

removeAllSyncTasks();
ContentResolver.setIsSyncable(mAccount, CONTENT_AUTHORITY, 1);
ContentResolver.setSyncAutomatically(mAccount, CONTENT_AUTHORITY, true);
ContentResolver.addPeriodicSync(mAccount, CONTENT_AUTHORITY, Bundle.EMPTY, SYNC_FREQUENCY);

Manifest part:

        <service
            android:name=".data.sync.SyncService"
            android:exported="true"
            android:process=":sync">
            <intent-filter>
                <action android:name="android.content.SyncAdapter"/>
            </intent-filter>
            <meta-data android:name="android.content.SyncAdapter"
                android:resource="@xml/syncadapter" />
        </service>


        <provider
            android:name=".data.provider.LevensContentProvider"
            android:authorities="@string/authority"
            android:label="@string/app_name_sync"
            android:exported="false"
            android:syncable="true" />

Syncadapter xml:

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="@string/authority"
    android:accountType="com.google"
    android:userVisible="true"
    android:supportsUploading="true"
    android:allowParallelSyncs="false"
    android:isAlwaysSyncable="true"/> 

When I manually start the sync. The Syncservice is also not starting at the Samsung tablets (it works on all other devices).

回答1:

It turns out it had nothing to do with Samsung / OS version...

The constructor of my SyncHelper was:

 public SyncHelper(Context context, String accountName) {
        Account account = null;
        Account[] accounts = AccountManager.get(context).getAccounts();
        for (Account acc : accounts) {
            if(acc.name.equals(accountName)){
                account = acc;
            }
        }
        if(account == null){
            throw new InvalidParameterException("Account not found");
        }
        init(context, account);
    }

This does not check for the type of account. There was an account of type com.evernote in the list and this was used to sync and that ofcourse won't work.

Added this to solve it:

 public SyncHelper(Context context, String accountName) {
        Account account = null;
        Account[] accounts = AccountManager.get(context).getAccounts();
        for (Account acc : accounts) {
            if(acc.name.equals(accountName) && acc.type.equals(ACCOUNT_TYPE)){
                account = acc;
            }
        }
        if(account == null){
            throw new InvalidParameterException("Account not found");
        }
        init(context, account);
    }

Now I can start bumping my head against the wall... ;-)