Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of LocalBroadcastManager without Android support library

There are lots of examples on StackOverflow and elsewhere that use the class LocalBroadcastManager to avoid broadcasting events outside of an app.

However, this class uses the Android support library as shown in the package name: android.support.v4.content.LocalBroadcastManager.

Is there an equivalent of LocalBroadcastManager in the standard SDK that does not use the Android support library?

It does not seem that the sendBroadcast method in android.content.Context has this kind of security granularity.

like image 565
Vincent Hiribarren Avatar asked Jan 28 '15 09:01

Vincent Hiribarren


People also ask

When should I use LocalBroadcastManager?

LocalBroadcastManager is used to register and send a broadcast of intents to local objects in your process. It has lots of advantages: You broadcasting data will not leave your app. So, if there is some leakage in your app then you need not worry about that.

What is a LocalBroadcastManager?

LocalBroadcastManager is an application-wide event bus and embraces layer violations in your app: any component may listen events from any other.


1 Answers

No it doesn't exist, If you want to recreate this class you can read the source code to implement yourself without using the support lib. Anyway, what's the problem using the support lib? it's lightweight.

A bit workaround could be using the normal BroadCastReceiver and put <android:exported="false"> on your manifest, inside this receiver, this avoid to other apps send you intents, so you are faking a local receiver.

Note: I said faking, because the LocalBroadcastManager has optimizations, by not spread the intent to the system...

Hope this helps.

like image 157
Sulfkain Avatar answered Oct 19 '22 15:10

Sulfkain