androidandroid-syncadapterlocalbroadcastmanager

Unable to send sendBroadcast from SyncAdapter to Activity


I have an application with a SyncAdapter that runs fine. The SyncService runs once per hour.

My application has a main FragmentActivity called DeviceControlActivity and I would like to receive messages from the SyncAdaptor, such as the syncResult.

I've tried a lot to get it up and running, but I'm not able to receive any messages on DeviceControlActivity from SyncAdapter.

In my DeviceControlActivity I do:

private BroadcastReceiver mSyncMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "Got it!");
    }
};

@Override
protected void onResume() {
    super.onResume();
    // Receive feedback from syncManager
    LocalBroadcastManager.getInstance(getApplication()).registerReceiver(mSyncMessageReceiver,
            new IntentFilter(Constants.MESSAGE_SYNC));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mSyncMessageReceiver);
}

On the SyncAdapter, I have:

private LocalBroadcastManager mBroadcastManager;

public SyncAdapter(Context context, boolean autoInitialize) {
    super(context, autoInitialize);


    mBroadcastManager = LocalBroadcastManager.getInstance(context);
}

@Override
public void onPerformSync(
        Account account,
        Bundle extras,
        String authority,
        ContentProviderClient provider,
        SyncResult syncResult) {

    Intent intent = new Intent(Constants.MESSAGE_SYNC);
    mBroadcastManager.sendBroadcast(intent);

I can send messages from DeviceControlActivity to itself and it works.

I can receive messages from the SyncService, but using an alternative way:

final int mask = ContentResolver.SYNC_OBSERVER_TYPE_PENDING | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE;
mSyncMonitor = ContentResolver.addStatusChangeListener(mask, this);

This listener reports the sync activity as expected.

But I would like to use a LocalBroadcastManager to communicate between the Service and the Activity, but I'm not able.

I've already check related questions like How to use LocalBroadcastManager?


Solution

  • According to the LocalBroadcastManager documentation this class is a Helper to register for and send broadcasts of Intents to local objects within your process.

    The above example dos not work if the <service> runs on a different process.

    The AndroidManifest.xml was:

    <service
                android:name=".sync.SyncService"
                android:exported="true"
                android:process=":sync">
    

    This is the way suggested in Creating a Sync Adapter.

    If we change the <service> declaration to:

        <service
            android:name=".sync.SyncService"
            android:exported="true">
    

    removing the android:process property, the SyncServicewill run in the same process and the LocalBroadcastManager will work.