5

I've got this app, in which users update certain variables in an Activity, and this Activity passes the new variables to a IntentService using a BroadcastReceiver. However, the BroadcastReceiver in the IntentService doesn't seem to be receiving the broadcasts. This is the code within the service to create the broadcast receiver

protected class UpdateReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent){
        Log.d("receiver", "Got message: ");
        //state=3;
        //Toast.makeText(context, "got it", Toast.LENGTH_SHORT).show();
    }
};

And here's the code to register the receiver, in the onHandle() function

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("updates");
UpdateReceiver lol = new UpdateReceiver();
DetectService.this.registerReceiver(lol, intentFilter);

Finally here's the code to send the broadcast, from the activity

Intent broadcastIntent = new Intent();
broadcastIntent.setAction("updates");
HomePageActivity.this.sendBroadcast(broadcastIntent);
Log.d("sender", "send msg");

When I put the receiver in the same activity as the broadcasting part, it works, but not when I put it into the IntentService. Help please!

On another related note, I've tried using LocalBroadcastManager in this project since the broadcasts are all local but eclipse doesn't seem to be able to import the compatibility class. I've installed it using Android SDK manager already. Is there any thing I'm doing wrong here?

Kara
  • 6,115
  • 16
  • 50
  • 57
Tan Jing Yuan
  • 73
  • 2
  • 7
  • are you declaring the receiver in your manifest? – dldnh Mar 09 '12 at 17:27
  • ok when i put the receiver in the same activity as the broadcasting part AND registered in manifest, the receiver receives but it also crashes the program. the note here seems to imply you either register in manifest or in e program but not both. http://developer.android.com/reference/android/content/Context.html#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter) – Tan Jing Yuan Mar 10 '12 at 00:36

2 Answers2

4

this Activity passes the new variables to a IntentService using a BroadcastReceiver.

That makes no sense. Use startService() to send a command to an IntentService. And an IntentService should not have a BroadcastReceiver, because the IntentService will be destroyed as soon as onHandleIntent() completes and therefore will never receive the broadcast.

I've tried using LocalBroadcastManager in this project since the broadcasts are all local but eclipse doesn't seem to be able to import the compatibility class.

:: shrug ::

Here is a sample project with Eclipse project files that uses LocalBroadcastManager. I encountered no particular Eclipse issues when creating the project.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2
protected class UpdateReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent){
        Log.d("receiver", "Got message: ");
        //state=3;
        //Toast.makeText(context, "got it", Toast.LENGTH_SHORT).show();
    }
};

In the onCreate() method or where relevant.

mReceiver = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("<your receivers intent goes here>");
this.registerReceiver(mReceiver, filter);

Now you should be able to send a broadcast and it be picked up.

Intent intent = new Intent("<your receivers intent goes here>");
// Add what you want to add to the intent right here.
<context-handle>.sendBroadcast(intent);
jjNford
  • 5,170
  • 7
  • 40
  • 64
  • 1
    tried it, it doesn't work. perhaps i should implement as service rather than intentservice.. – Tan Jing Yuan Mar 10 '12 at 00:54
  • @TanJingYuan it should work, I use this often. What have you defined in your manifest? Make sure you have android:enabled="true" set for the receiver. Can you post your manifest? – jjNford Mar 10 '12 at 03:16
  • You're right, it should probably work, but based on the other answer below, it seems the issue isn't with the Receiver, but rather with the fact that I'd implemented my service as an IntentService rather than as a Service. – Tan Jing Yuan Mar 20 '12 at 09:46
  • @TanJingYuan Have you tried declaring the receiver in the manifest, the system can instantiate it also? – jjNford Mar 20 '12 at 11:14