You can use LocalBroadcasts
instead of normal broadcasts. They essentially are like real broadcasts, but only visible to one application. I assume you want to communicate from a Service
to the app? Then LocalBroadcasts
should be exactly what you are looking for, but without knowing exactly how you implemented anything I cannot give you very specific advice.
Anyway if you want to use LocalBroadcasts
you first have to create a BroadcastReceiver
like you would with a normal broadcast:
private static final String SOME_ACTION = "someAction";
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(SOME_ACTION.equals(action)) {
// Do your work
}
}
};
You can then register and unregister the BroadcastReceiver
like this:
@Override
public void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(SOME_ACTION);
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.unregisterReceiver(broadcastReceiver);
}
And finally you can send a broadcast from your Service
or anywhere else in your application like this:
Intent intent = new Intent(SOME_ACTION);
LocalBroadcastManager manager = LocalBroadcastManager.getInstance(getActivity());
manager.sendBroadcast(intent);