I faced to one problem on android 4.0.3 (on 4.1.2 it works fine). I have in my Activity BroadcastReceiver. When I send a broadcast, method onReceive() called always twice. Please give me any suggestions about BroadcastReceiver differences on 4.0 and 4.1
private final GcmBroadcastReceiver gcmReceiver = new GcmBroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); if (action != null && action.equals(MyBroadcastReceiver.ACTION)) { Log.d("tag", intent.getStringExtra("alert")); } } }; }; @Override protected void onPause() { unregisterReceiver(gcmReceiver); super.onPause(); } @Override protected void onResume() { super.onResume(); registerGcmReceiver(); } private void registerGcmReceiver() { IntentFilter filter = new IntentFilter(MyBroadcastReceiver.ACTION); filter.setPriority(2); filter.addCategory("com.android.mypackage"); registerReceiver(gcmReceiver, filter); }
Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.
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.
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.
Only onReceive() method is called in BroadcastReciver's life cycle. Show activity on this post. A BroadcastReciever life cycle ends (ie stop receiving broadcast) when you unregister it. usually you would do this in the onPause/onStop method.
Usually onReceive is being called twice since people are registering the broadcast in 2 locations, Most likely you are registering in your onCreate and in your onResume. (Choose one spot to register).
Although you might have done it probably a dozen of times - it is always recommended to take another glance at the Activity Life Cycle.
I had same issue.
onReceive()
gets called twice because i was registering Broadcast receiver twice. One in my activity's onCreate()
and another in manifest
.
I remove broadcast receiver registration from onCreate()
and it's working fine. Register your broadcast receiver in only one of them.
There are two ways to do this:
Which method (static or dynamic) to use when depends completely upon what you’re trying to do. Basically when you want to do some changes right on the screen (home screen, launcher, status bar, etc.) by showing up some notification or some indicator in the status bar by listening to system wide events or maybe those sent by other apps, then it make sense to use statically registered broadcast receivers. Whereas based on similar events you want to do changes right in your app when the user is using it or maybe it’s put in the background, then it makes sense to use dynamically registered receivers which’ll last till the registering components are destroyed.
For more info: http://codetheory.in/android-broadcast-receivers/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With