I have declared my receiver in my manifest:
<receiver
android:name=".MyTestReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.ACTION_TEST"/>
</intent-filter>
</receiver>
And here is my MyTestReceiver class:
public class MyTestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if ("com.example.ACTION_TEST".equals(action)) {
Toast.makeText(context, "Test!", Toast.LENGTH_SHORT).show();
}
}
}
But when I execute this code from elsewhere within my app:
Intent intent = new Intent("com.example.ACTION_TEST");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
...the local broadcast is not received (i.e., the toast is not shown).
Questions:
- Is it possible to register a local broadcast receiver in the manifest?
- If so, have I declared my local broadcast receiver incorrectly?
- If it's not possible to declare a local broadcast receiver in the manifest, and I declare it in my
Application
subclass instead, will it have the same 'scope' as a receiver declared in the manifest? (I mean, will it receive broadcasts in all the same conditions/situations as it would if it was declared in the manifest?) - If there is a difference between specifying the receiver in the manifest than in my
Application
subclass, would I need to use general (non-local) broadcasts rather than local broadcasts? (In the actual app, the local broadcast will be sent when my IntentService completes its work. The IntentService will be triggered by an FCM push message.)
NB - All I can seem to find about this in the documentation is:
Note: To register for local broadcasts, call
LocalBroadcastManager.registerReceiver(BroadcastReceiver, IntentFilter)
instead.
...which doesn't address the main issue of whether or not you can specify the receiver in the manifest.