Question

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?

Was it helpful?

Solution

Either use LocalBroadcastManager in both places (sendBroadcast() and registerReceiver()), or do not use LocalBroadcastManager at all. Right now, you have a mismatched pair.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top