Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BroadcastReceiver onReceive() called twice on android 4.0

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); } 
like image 731
girlOnSledge Avatar asked Jan 23 '14 16:01

girlOnSledge


People also ask

What is the role of the onReceive () method in the BroadcastReceiver?

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.

What is BroadcastReceiver Android?

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.

Which are the BroadcastReceiver are available in Android?

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.

What is the life cycle of BroadcastReceiver in Android?

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.


2 Answers

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.

like image 121
Mercury Avatar answered Sep 20 '22 04:09

Mercury


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:

  • Statically in the manifest file.
  • Dynamically in the code.

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/

like image 34
Sushant Avatar answered Sep 21 '22 04:09

Sushant