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?