0

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.

Tooroop
  • 1,824
  • 1
  • 20
  • 31

2 Answers2

1

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();
}
}
  • I have it like that now. But i need to return mMessenger.getBinder() to handle communication between the activtiy and service. Here is the doc: https://developer.android.com/guide/components/bound-services.html#Messenger – Tooroop Jul 07 '16 at 10:30
1

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

Community
  • 1
  • 1
Marten
  • 3,802
  • 1
  • 17
  • 26
  • Thanks @Marten. This could work and is probably the solution. I'll try and implement that and I'll get back to you on that. – Tooroop Jul 13 '16 at 06:12