Bind SyncAdapter service to Activity

678 Views Asked by At

I'm currently working with a SyncAdapter that syncs data every 15 minutes. Is it possible to bind that Service to the Activity so I can show the user what the currenty sync status is?

I read in the documentation about IPC between services and activities that you can communicate with Messengers and Handlers. But I can't return a mMessenger.getBinder() in my SyncService onBind method because I have to return the syncAdapter.getSyncAdapterBinder() because of the SyncAdapter.

2

There are 2 best solutions below

1
Marten On BEST ANSWER

You can return a different IBinder, depending on the action that was set for the Intent. Check out my response to Android SyncAdapter: how to get notified of specific sync happened

1
manishkaushik On

Try this definetiley work.

public class SyncService extends Service {

private static SyncAdapter sSyncAdapter = null;

private static final Object sSyncAdapterLock = new Object();

@Override
public void onCreate() {

    synchronized (sSyncAdapterLock) {
        if (sSyncAdapter == null) {
            sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
        }
    }
}

@Override
public IBinder onBind(Intent intent) {

    return sSyncAdapter.getSyncAdapterBinder();
}
}