unable to get data via broadcast from service to fragment [duplicate] - android

I'm trying to receive broadcasts from a service with 2 different receivers. One receiver update's a view so I register it in the activity's onResume method.
When the app is not in the foreground I use the other receiver so I can show a system notification when the background service completes.
The code below is how I'm registering my receivers:
#Override
protected void onPause() {
// unregister local
unregisterReceiver(localReceiver);
// register remote
registerReceiver(remoteReceiver, filter);
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
// remove remote receiver
// since remote is only registered in onPause it won't be registered during the first onResume call
// so we want to ignore any exceptions
try {
unregisterReceiver(remoteReceiver);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "No receiver registered, could be first time");
}
// add local receiver
registerReceiver(localReceiver, filter);
Log.i(LOG_TAG, "resumed. should be registered");
}
The two receiver's are instantiated like this at the top of the Activity class:
BroadcastReceiver localReceiver = new BroadcastReceiver() { ... };
WaitTimeReceiver remoteReceiver = new WaitTimeReceiver();
The Service makes the intent as:
broadcastIntent = new Intent(Support.SERVICE_BR);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
// later on sends using
sendBroadcast(broadcastIntent);
The Filter in the Activity is made to match:
filter.addAction(Support.SERVICE_BR);
filter.addCategory(Intent.CATEGORY_DEFAULT);
Everything above is working the pause resume functionality works as expected, but my question is why the LocalBroadcastManager was not?
Using LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this) and then calling lbm.registerReceiver(localReceiver) was not receiving any of my broadcasts for either.
Why wasn't the LocalBroadcastManager receiving any of my broadcasts?

BroadcastReceivers registered with LocalBroadcastManager can only receive broadcasts sent with LocalBroadcastManager. Broadcasts sent with an Activity's or Service's sendBroadcast() method cannot be received by LocalBroadcastManager Receivers.
Use the LocalBroadcastManager#sendBroadcast() method instead. For example:
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)

Related

Android Local broadcast is not working from JobService [duplicate]

I'm trying to receive broadcasts from a service with 2 different receivers. One receiver update's a view so I register it in the activity's onResume method.
When the app is not in the foreground I use the other receiver so I can show a system notification when the background service completes.
The code below is how I'm registering my receivers:
#Override
protected void onPause() {
// unregister local
unregisterReceiver(localReceiver);
// register remote
registerReceiver(remoteReceiver, filter);
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
// remove remote receiver
// since remote is only registered in onPause it won't be registered during the first onResume call
// so we want to ignore any exceptions
try {
unregisterReceiver(remoteReceiver);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "No receiver registered, could be first time");
}
// add local receiver
registerReceiver(localReceiver, filter);
Log.i(LOG_TAG, "resumed. should be registered");
}
The two receiver's are instantiated like this at the top of the Activity class:
BroadcastReceiver localReceiver = new BroadcastReceiver() { ... };
WaitTimeReceiver remoteReceiver = new WaitTimeReceiver();
The Service makes the intent as:
broadcastIntent = new Intent(Support.SERVICE_BR);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
// later on sends using
sendBroadcast(broadcastIntent);
The Filter in the Activity is made to match:
filter.addAction(Support.SERVICE_BR);
filter.addCategory(Intent.CATEGORY_DEFAULT);
Everything above is working the pause resume functionality works as expected, but my question is why the LocalBroadcastManager was not?
Using LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this) and then calling lbm.registerReceiver(localReceiver) was not receiving any of my broadcasts for either.
Why wasn't the LocalBroadcastManager receiving any of my broadcasts?
BroadcastReceivers registered with LocalBroadcastManager can only receive broadcasts sent with LocalBroadcastManager. Broadcasts sent with an Activity's or Service's sendBroadcast() method cannot be received by LocalBroadcastManager Receivers.
Use the LocalBroadcastManager#sendBroadcast() method instead. For example:
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)

Different way to register and unregistered broadcast receiver

I am trying code for LocalBroadcastManager.While register and unregister BroadcastReceiver , I am using below code.
Can any one give difference between both way to register and unregister
LocalBroadcastManager?
First Way:
...
//Register receiver
registerReceiver(mPairingReceiver, IntentFilter filter = new IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST"));
...
#Override
protected void onStop() {
super.onStop();
// unregister receiver
unregisterReceiver(mPairingReceiver);
}
Second Way:
...
//Register receiver
LocalBroadcastManager.getInstance(MainActivity.this).registerReceiver(mPairingReceiver, IntentFilter filter = new IntentFilter("android.bluetooth.device.action.PAIRING_REQUEST"));
...
#Override
protected void onStop() {
super.onStop();
// unregister receiver
LocalBroadcastManager.getInstance(MainActivity.this).unregisterReceiver(mPairingReceiver);
}
Your first snippet calls unregisterReceiver() on a Context. This unregisters a receiver that you registered via registerReceiver() on the same Context. These methods are for system-level broadcasts.
Your second snippet calls unregisterReceiver() on a LocalBroadcastManager. This unregisters a receiver that you registered via registerReceiver() on the same LocalBroadcastManager. These methods are for local broadcasts, solely within your own application.

Dynamic BroadcastReceivers: LocalBroadcastManager.registerReceiver vs registerReceiver

I'm trying to receive broadcasts from a service with 2 different receivers. One receiver update's a view so I register it in the activity's onResume method.
When the app is not in the foreground I use the other receiver so I can show a system notification when the background service completes.
The code below is how I'm registering my receivers:
#Override
protected void onPause() {
// unregister local
unregisterReceiver(localReceiver);
// register remote
registerReceiver(remoteReceiver, filter);
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
// remove remote receiver
// since remote is only registered in onPause it won't be registered during the first onResume call
// so we want to ignore any exceptions
try {
unregisterReceiver(remoteReceiver);
} catch (IllegalArgumentException e) {
Log.e(LOG_TAG, "No receiver registered, could be first time");
}
// add local receiver
registerReceiver(localReceiver, filter);
Log.i(LOG_TAG, "resumed. should be registered");
}
The two receiver's are instantiated like this at the top of the Activity class:
BroadcastReceiver localReceiver = new BroadcastReceiver() { ... };
WaitTimeReceiver remoteReceiver = new WaitTimeReceiver();
The Service makes the intent as:
broadcastIntent = new Intent(Support.SERVICE_BR);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
// later on sends using
sendBroadcast(broadcastIntent);
The Filter in the Activity is made to match:
filter.addAction(Support.SERVICE_BR);
filter.addCategory(Intent.CATEGORY_DEFAULT);
Everything above is working the pause resume functionality works as expected, but my question is why the LocalBroadcastManager was not?
Using LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this) and then calling lbm.registerReceiver(localReceiver) was not receiving any of my broadcasts for either.
Why wasn't the LocalBroadcastManager receiving any of my broadcasts?
BroadcastReceivers registered with LocalBroadcastManager can only receive broadcasts sent with LocalBroadcastManager. Broadcasts sent with an Activity's or Service's sendBroadcast() method cannot be received by LocalBroadcastManager Receivers.
Use the LocalBroadcastManager#sendBroadcast() method instead. For example:
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)

Where to call unregisterReceiver in AccessibilityService

I'm a bit confused. An AccessibilityService can get all new incomming notifications and send information with a Broadcast to a BroadcastReceiver. My AccessibilityService does so
public void onServiceConnected() {
// ...
Communication c = new Communication();
IntentFilter filter = new IntentFilter();
filter.addAction("com.cilenco.lockscreen.notification.send");
registerReceiver(c, filter);
}
Intent intent=new Intent("com.cilenco.lockscreen.notification.send");
intent.putExtra("string1", string1);
intent.putExtra("string2", string2);
sendBroadcast(intent);
After I send the Broadcast the AccessibilityService is still alive. If a new notification is detected onServiceConnected is called again but then the Reciever is connected again in I never called
unregisterReceiver(c);
Where do I have to call this?
You call unregisterReceiver() when you no longer want to receive broadcasts. Convention is that it is called in your onPause(). Or sonner if you have no need for it anymore.
You should call unregisterReceiver() when you no longer need to listen for intents or when your service's onDestroy() is called, whichever comes first.

how can I notify a running activity from a broadcast receiver?

I have an activity, it needs to response to a broadcast event.
Since an activity can not be a broadcast receiver at the same time,
I made a broadcast receiver.
My question is: how can I notify the activity from the broadcast receiver?
I believe this is a common situation, so is there a design pattern for this?
The broadcast is the notification. :) If you want to say, start an activity or a service, etc., based on a received broadcast then you need a standalone broadcast receiver and you put that in your manifest file. However, if you want your activity itself to respond to broadcasts then you create an instance of a broadcast receiver in your activity and register it there.
The pattern I use is:
public class MyActivity extends Activity {
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(...) {
...
}
});
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction(BROADCAST_ACTION);
this.registerReceiver(this.receiver, filter);
}
public void onPause() {
super.onPause();
this.unregisterReceiver(this.receiver);
}
}
So, this way the receiver is instantiated when the class is created (could also do in onCreate). Then in the onResume/onPause I handle registering and unregistering the receiver. Then in the reciever's onReceive method I do whatever is necessary to make the activity react the way I want to when it receives the broadcast.

Categories

Resources