MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Log.i("MyReceiver", "MyAction received!");
}
}
In AndroidManifest.xml
(under the application
tag)
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="MyAction" />
</intent-filter>
</receiver>
MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendBroadcast(new Intent("MyAction"));
}
}
MyReceiver.onReceive
method is never triggered.
Did I miss something?
Intent
actions in the manifest on Android 8.0+. What version of Android are you testing on? Also, if bothMainActivity
andMyReceiver
are in the same app, there are better options for in-process messages, such asLocalBroadcastManager
, greenrobot's EventBus, RxJava-based event buses, etc. – TorbertBroadcastReceiver
works. Furthermore, I don't want to declare my receiver programatically since theMyReceiver
will have some system actions in itsintent-filter
tag. I really want it to be declared in the manifest (as long as it's possible). – Sink