Reputation: 599
I send a broadcast after i get a push notification
within my onMessageReceived()
method.
in BroadcastReceiver's onReceive()
method I want to display an AlertDialog
that allows the user to move to another fragment.
The problem is that my onReceive()
method is called 3 times, instead of 1 and my AlertDialog
opens 3 times.
I tried something like:
private int onReceiveCalledIndex = 0;
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getExtras() != null)
{
String requestIdString = (String) intent.getExtras().get(Constants.PUSH_NOTIFICATION_CHAT_PUT_EXTRA);
Log.e("DEBUG_RECEIVE",String.valueOf(onReceiveCalledIndex));
onReceiveCalledIndex += 1;
if(onReceiveCalledIndex == 2 ) showNewMessageDialog(requestIdString);
}
}
But I get these logs and my index is never incremented.It's like the method is called on three different threads simultaneously ...
E/DEBUG_RECEIVE: 0
E/DEBUG_RECEIVE: 0
E/DEBUG_RECEIVE: 0
Someone please tell me a way to make the onReceive()
to be called just once or to handle so I will open only one dialog.
Upvotes: 0
Views: 994
Reputation: 599
It seams that if you register the receiver from within the activity instead of the fragment all works just fine.
Thanks for help.
Upvotes: 0
Reputation: 11
Register and Unregister broadcast receiver dynamically
To register use:-
this.registerReceiver(broadCastReceiver, new IntentFilter(droid.intent.action.TIME_TICK'));
To unregister:-
this.unregisterReceiver(broadCastReceiver);
Upvotes: 1