I send a Broadcast by doing:
Intent intent = new Intent("com.usmaan.myApp.DATA_RECEIVED");
intent.putExtra("matchId", newRowId);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
This is the Service that wraps up the AsyncTask
which runs Broadcasts the above:
<service
android:name=".services.DataService"
android:exported="false" />
In my Activity, I register a Receiver
in onResume
:
IntentFilter intentFilter = new IntentFilter("com.usmaan.myApp.DATA_RECEIVED");
registerReceiver(mDataReceiver, intentFilter);
The `BroadReceiver looks like this:
private class DataReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final long matchId = intent.getLongExtra("matchId", 0);
Toast.makeText(LaunchActivity.this, "" + matchId, Toast.LENGTH_LONG);
}
}
The onReceive
is never fired. What am I doing wrong?
Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.
The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context.
It's always suggested to register and unregister broadcast receiver programmatically as it saves system resources.
Either use LocalBroadcastManager
in both places (sendBroadcast()
and registerReceiver()
), or do not use LocalBroadcastManager
at all. Right now, you have a mismatched pair.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With