Question

I'm trying to synchronize my local database with my server using AbstractThreadedSyncAdapter. I've followed a guide I found, but I can't get it working. It's for Android 2.2.

I have my Authenticator system working. In my AccountAuthenticatorActivity, after creating the account, I do:

Bundle params = new Bundle();
params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
ContentResolver.addPeriodicSync(account, "com.syncal.authority", params, 30);
ContentResolver.setSyncAutomatically(account, "com.syncal.authority", true);
ContentResolver.requestSync(account,"com.syncal.authority",params);

I created my SyncService and I declare it in my manifest. Some of the functions:

@Override
public IBinder onBind(Intent intent) {
    Log.i(TAG, "inBind() " + intent.getAction());
    return getSyncAdapter().getSyncAdapterBinder();
}

private synchronized AbstractThreadedSyncAdapter getSyncAdapter() {
    Log.i(TAG, "getSyncAdapter()");
    if (mSyncAdapter == null) {
         mSyncAdapter = new SweetSyncAdapter(this, true);
    }
    return mSyncAdapter;
}

And then I implemented my SyncAdapter. The onPerformSync function is empty, I just want to know if it's called or not, and it's NEVER called.

public class SweetSyncAdapter extends AbstractThreadedSyncAdapter {

    Context mContext;
    private String AUTH_TOKEN_TYPE;
    private AccountManager mAccountManager;
    private String mAuthToken;
    private final String TAG = "SweetContactSync";
    static final String LAST_SYNC_KEY = "lastSync";

    public SweetSyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
        Log.i(TAG, "SweetSyncAdapter");
        mContext = context;
        AUTH_TOKEN_TYPE = mContext.getString(R.string.account_type);
        mAccountManager = AccountManager.get(mContext);
    }

    public interface ISugarRunnable {
         public void run() throws URISyntaxException, OperationCanceledException, AuthenticatorException, IOException,
            AuthenticationException;
    }

    class SugarRunnable implements Runnable {
        ISugarRunnable r;
        Account mAccount;
        SyncResult mSyncResult;

        public SugarRunnable(Account acc, SyncResult syncResult, ISugarRunnable _r) {
            r = _r;
            mAccount = acc;
            mSyncResult = syncResult;
        }

        public void run() {
            try {
                r.run();
    } catch ... //All the catchs
        }
    }

    @Override
    public void onPerformSync(final Account account, Bundle extras, String authority,
                ContentProviderClient provider, SyncResult syncResult) {
        Log.i(TAG, "onPerformSync()");
    }
}

My xml/sync_adapter.xml has the following code:

<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.syncal.authority"
    android:accountType="com.syncal.account"   
/>

Here's the service part of the manifest:

<service android:name=".sweet.SweetSyncService" android:exported="true"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.content.SyncAdapter" />
    </intent-filter>

    <meta-data android:name="android.content.SyncAdapter"
        android:resource="@xml/sync_adapter" />

</service>

I even created my own ContentProvider (the functions are empty, they just write in the log) because the guide I followed said it was needed and I declared it in the manifest too, but none of the functions are called.

Does someone know why my onPerformSync function isn't called? The onBind function of my SweetSyncService is called and it creates my SweetSyncAdapter, but my log doesn't show any message from here on.

Thanks for reading, I know I typed a lot of code, but I've been working on it for a while and I can't manage to fix it.

Was it helpful?

Solution

I've finally managed to find the error. I've added some permissions in my manifest and now it works:

<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top