I know this is a popular topic, but I could not find a case that fits mine. Looking to get some modularity in my design I created a single broadcast receiver that would listen for any network state change and act as a generic gateway for (connectivity for now) broadcasts into my app. This class is called ConnectivityReceiver and it is basically this:
public class ConnectivityReceiver extends BroadcastReceiver {
private static final String TAG = "ConnectivityReceiver";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"onReceive called");
Log.d(TAG,"Intent.getAction() is "+intent.getAction());
if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
Intent out = new Intent(MapFragment.ACTION_LOCAL_CONNECTIVITY_CHANGE);
Log.d(TAG,"Sending broadcast");
context.sendBroadcast(out);
}
}
}
I did register it in the Manifest and it is working pretty well. Then, I have a fragment that would be interested to know about this kind of connectivity changes, so I created a simple inner BroadcastReceiver that looks like this:
public BroadcastReceiver connectivityReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"got connection info!");
}
};
This one is not registered in the Manifest, but I did care enough to register and unregister it in the fragment's onCreate and onDestroy methods respectively. Something like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG,"onCreate");
connectivityReceiver = new ConnectivityReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MapFragment.ACTION_LOCAL_CONNECTIVITY_CHANGE);
this.getActivity().registerReceiver(connectivityReceiver, intentFilter);
/* Creating a ParseHandler, this will be needed for the PathParser */
mParserHandler = new ParserHandler(getActivity());
}
and
@Override
public void onDestroy(){
Log.d(TAG,"onDestroy");
this.getActivity().unregisterReceiver(connectivityReceiver);
}
But for some unknown reason the second broadcast receiver in the chain is not getting the broadcast! It is being issued, since the first one (the one registered in the Manifest) is getting it.
Any ideas? the custom action name looks like this:
public static final String ACTION_LOCAL_CONNECTIVITY_CHANGE = "com.spiral.android.transito.CONNECTIVITY_CHANGE";
.
stackoverflow.comm
No comments:
Post a Comment