Is there anyway to register a BroadcastReceiver in AndroidManifest.xml and receives broadcast that is send by a LocalBroadcastManager?
Currently I must call
registerReceiver(BroadcastReceiver receiver, IntentFilter filter)
to register a Receiver, declare in AndroidManifest.xml won't work. But this means I must know exactly the receiver's package name and class name, not just the intent filter. Is it possible to declare the receiver in the manifest file?
following is my current code.
AndroidManifest.xml:
... <receiver android:name="com.example.test.MessageReceiver" android:enabled="true" > <intent-filter> <action android:name="com.m2x.test.intent.MESSAGE_RECEIVED" /> </intent-filter> </receiver> ...
MainActivity.java:
Intent intent = new Intent(); intent.setAction("com.m2x.test.intent.MESSAGE_RECEIVED"); LocalBroadcastManager manager = LocalBroadcastManager.getInstance(mContext.get()); manager.sendBroadcast(intent);
MessageReceiver.java
public class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("com.m2x.test.intent.MESSAGE_RECEIVED")) { Toast.makeText(context, "user message received", Toast.LENGTH_SHORT).show(); } } }
You register this broadcast receiver either in the manifest file or in the code. These events are intents. So, whenever these kinds of events happen, an intent is triggered.
public boolean isReceiverRegistered(BroadcastReceiver receiver); It would return true if it's registered and false if not.
No, you can't.
The local BroadcastReceiver
isn't a real BroadcastReceiver
, basically its a list of callbacks
functions.
You can check the source code of LocalBroadcastManager.java.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With