Reputation: 2403
i have an Activity where inside a method a send a broadcast to a custom receiver located in Activity's Fragment...i would like to set a permission as a second parameter of sendBroadcast so my receiver can receive only specific broadcasts...
Activity's sendBroadcast():
@Override
public void update(Observable observable, Object connectionStatus) {
Log.e(debugTag, connectionStatus+"");
intent = new Intent("networkStateUpdated");
intent.putExtra("connectivityStatus", (int) connectionStatus);
sendBroadcast(intent, "mypermission");
}
initialization of custom Receiver inside Fragment's onActivityCreated
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
connectionStatus = intent.getExtras().getInt("connectivityStatus");
}
};
@Override
public void onResume() {
super.onResume();
getActivity().registerReceiver(broadcastReceiver, new IntentFilter("networkStateUpdated"), "mypermission", null);
}
@Override
public void onPause() {
super.onPause();
getActivity().unregisterReceiver(broadcastReceiver);
}
setting "mypermission" as second parameter of sendBroadcast is apparently not working..
Upvotes: 0
Views: 243
Reputation: 1007659
Use system broadcasts (e.g., sendBroadcast()
called on Context
) when you need to send messages between processes.
Within a process, using system broadcasts adds IPC overhead and security concerns. Instead, use some sort of in-process event bus. Personally, I use greenrobot's EventBus. LocalBroadcastManager
is part of the Android Support libraries. Others prefer Square's Otto or other event bus implementations. These are less expensive in terms of overhead, and they are private to your app, so there are no new security concerns.
Upvotes: 1