Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver - onReceive Not Being Called

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?

like image 846
Subby Avatar asked May 04 '14 21:05

Subby


People also ask

What is the role of the onReceive () method in the BroadcastReceiver?

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.

Which method will be called when intent broadcast shall be received by BroadcastReceiver?

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.

Is it necessary to unregister BroadcastReceiver?

It's always suggested to register and unregister broadcast receiver programmatically as it saves system resources.


1 Answers

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

like image 124
CommonsWare Avatar answered Sep 29 '22 06:09

CommonsWare