68871

BroadcastReceiver in Singleton class not receiving intents

Question:

I have a Broadcast receiver in a singleton class that's not receiving Broadcast intents.

The singleton is initialized with a context (i.e. getInstance(context)).

I call getContext().sendBroadcast(intent); but the BroadcastReceiver doesn't get anything. I've confirmed the intent filters are matching.

The way I register the receiver is in my singleton constructor like so

private class Singleton(Context context) { context.registerReceiver(mReceiver, INTENT_FILTER); .... private onDestroy(Context context) { context.unregisterReceiver(mReceiver); ....

What's going on!?

Answer1:

OK so I figured out what I did wrong. The Singleton is being created by activity and hence the context being passed in is an <b>Activity Context.</b> Hence I needed to update my constructor to this:

private class Singleton(Context context) { context.getApplicationContext().registerReceiver(mReceiver, INTENT_FILTER);

Not sure why this makes a difference. My guess is that the activity context is destroyed and hence the BroadcastReceiver with it? Additional insight is welcome!

Recommend

  • How to pause the background music while recording audio in android
  • Update ProgressBar from a Service started by a BroadcastReceiver
  • How to pass value from activity to broadcastreceiver?
  • Saving to SD card in KitKat
  • TelephonyManager.CALL_STATE_RINGING calls twice while one call ringing
  • how to check if a field is not unique
  • Reading Excel files in a locale independent way
  • nodejs tls session id
  • Can't connect using mysql_connect to Database in ipage.com hosting?
  • INSTALL_REFERRER not received on production
  • PHP MySQL generating unique random number
  • How to detect airplane mode especially in jelly beam?
  • Broadcast receiver with AIRPLANE_MODE not working with SDK 26
  • Parse returned C# list in AJAX success function
  • ssh2_scp_send() using php corrupts pdf
  • Cannot access sitecore item field via API
  • Android Sms Broadcast Listener not working
  • is it bad to use public static fields/variables?
  • How to search for a record and then delete it
  • Move an object (element) one step up with Javascript
  • PHP PDF generation problem
  • Zend Framework 2 - Building a simple form with Validators
  • Process.PrivateMemorySize64 returning committed memory instead of private
  • How to run .java file with Variable Name
  • Align Excel cell to center VB - xlCenter is not declared
  • Singleton Alternative - is it equivalent?
  • Thrust filter by key value
  • MATLAB - Classification output
  • Imports in __init__.py and `import as` statement
  • Escaping single quotes in JDBC with MySql
  • Android NFC read Tags issue. Activity starts each time on data received
  • p:fileDownload in p:dataTable does not work (just refreshes page) after performing search on the p:d
  • Send data from edittext to listview
  • Embedded Google Maps in Rails not responsive
  • Adding Parent and Child Nodes in TreeView from Sql Server 2008
  • How can go in last activity with resume button?
  • Android application: how to use the camera and grab the image bytes?
  • MS Access - How to change the linked table path by amend the table
  • Dialing with Intent.ACTION_CALL stopps at # in phone number
  • Getting last autonumber in access