BroadcastReceiver on a service not receiving Android broadcasts

I have this application where users update certain variables in an Activity, and this Activity passes new variables to the IntentService using a BroadcastReceiver. However, the BroadcastReceiver in the IntentService doesn't seem to receive broadcasts. This is the code inside 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 is 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 action

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 broadcast part it works, but not when I put it in the IntentService. Help me please!

In another related post, I tried to use LocalBroadcastManager on this project as broadcasts are local, but eclipse can't seem to import the compatibility class. I've already installed it using Android SDK manager. Is there something I am doing wrong here?

+3


source to share


2 answers


this operation broadcasts new IntentService variables using the BroadcastReceiver.

This makes no sense. Use startService()

to send a command to IntentService

. It IntentService

shouldn't BroadcastReceiver

, because it IntentService

will be destroyed as soon as it onHandleIntent()

finishes and therefore never receives the broadcast.

I've tried using LocalBroadcastManager on this project as broadcasts are local, but eclipse can't seem to import the compatibility class.



:: shrug ::

Here is a sample project with Eclipse project files that uses LocalBroadcastManager

. I didn't have much trouble with Eclipse when creating the project.

+4


source


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 as needed.

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

      



Now you will be able to send and pick up the broadcast.

Intent intent = new Intent("<your receivers intent goes here>");
// Add what you want to add to the intent right here.
<context-handle>.sendBroadcast(intent);

      

+2


source







All Articles