Register a Local BroadcastReceiver in AndroidManifest.xml?

teejoe picture teejoe · Apr 29, 2014 · Viewed 9.9k times · Source

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();
        }

    }
}

Answer

JafarKhQ picture JafarKhQ · Apr 29, 2014

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.