I want to start a BroadcastReceiver
from an Activity
. How can I do that?
Define your BroadcastReceiver:
private final BroadcastReceiver receiver
= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Do something
}
};
Register your receiver in onResume:
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("SOME_ACTION");
registerReceiver(receiver, filter);
}
Unregister the receiver in onPause:
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}