Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver within a Service not receiving broadcasts Android

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?

like image 823
Tan Jing Yuan Avatar asked Mar 09 '12 17:03

Tan Jing Yuan


People also ask

How pass data from BroadcastReceiver to activity in Android?

getStringExtra("message"); And then you will use message as you need. If you simply want the ReceiveText activity to show the message as a dialog, declare <activity android:theme="@android:style/Theme. Dialog" /> in your manifest for ReceiveText and then set the message to a textview in the activity.

What is the difference between service and BroadcastReceiver in Android?

A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.

What is a BroadcastReceiver in Android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

Which are the BroadcastReceiver are available in Android?

There are mainly two types of Broadcast Receivers: Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.


2 Answers

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.

like image 178
CommonsWare Avatar answered Sep 27 '22 01:09

CommonsWare


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);
like image 38
jjNford Avatar answered Sep 25 '22 01:09

jjNford