在AndroidManifest.xml中注册一个本地BroadcastReceiver?

有没有办法在AndroidManifest.xml中注册BroadcastReceiver并接收由LocalBroadcastManager发送的广播?

目前我必须打电话

registerReceiver(BroadcastReceiver receiver, IntentFilter filter) 

要注册Receiver,在AndroidManifest.xml中声明将不起作用。 但是这意味着我必须确切地知道接收者的包名和类名,而不仅仅是意图filter。 是否有可能在清单文件中声明接收器?

以下是我目前的代码。

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

Solutions Collecting From Web of "在AndroidManifest.xml中注册一个本地BroadcastReceiver?"