How to use/locate LocalBroadcastManager
as described in google docs and Service broadcast doc?
I tried to google it, but there is no code available to s
I'd rather like to answer comprehensively.
LocalbroadcastManager included in android 3.0 and above so you have to use support library v4 for early releases. see instructions here
Create a broadcast receiver:
private BroadcastReceiver onNotice= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// intent can contain anydata
Log.d("sohail","onReceive called");
tv.setText("Broadcast received !");
}
};
Register your receiver in onResume of activity like:
protected void onResume() {
super.onResume();
IntentFilter iff= new IntentFilter(MyIntentService.ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, iff);
}
//MyIntentService.ACTION is just a public static string defined in MyIntentService.
unRegister receiver in onPause:
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(onNotice);
}
Now whenever a localbroadcast is sent from applications' activity or service, onReceive of onNotice will be called :).
Edit: You can read complete tutorial here LocalBroadcastManager: Intra application message passing