4

I have a service that get data from an other application.

When I get date I send message to broadCast in order to refresh the UI.

The onReceive method is called many times and data displayed multiple times.

this is my code:

DataService.java

if(sizeLat == 1) {
                                       
   sendMessage("Alerte1;");
                                    
}
else {

   sendMessage("Alerte2;");
}


 private void sendMessage(String message) {
    Log.w("","==> send message");
    Intent intent = new Intent("my-event");
    // add data
    intent.putExtra("message", message);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
 }

MainActivity.java

// handler for received Intents for the "my-event" event 
    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
          
                      
      Log.e("","Onreceiver");
      String message = intent.getStringExtra("message");
    
        
        if(message.equals("Alerte1")){
      parentItems.add(message);
      adapter.notifyDataSetChanged();
         }}};

@Override
  protected void onResume() {
    Log.d(TAG, "On Resume");
    super.onResume();
    
    // Register mMessageReceiver to receive messages.
    
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("my-event"));}

How can I resolve the problem ?

mrid
  • 5,782
  • 5
  • 28
  • 71
Wafae
  • 177
  • 3
  • 12
  • 1
    That problem can be of your service check whether your service sending you multiple data or may be sending data multiple time. Do debug and check it or print Log to see what is exactly happening. – Pankaj Apr 06 '15 at 12:12
  • no the service send just one time, I cheched that – Wafae Apr 06 '15 at 12:20
  • the methode sendmessage() is called juste one time but onReceive() is called many times – Wafae Apr 06 '15 at 12:23
  • 1
    You are craeting multiple instances of private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() , make it static or singleton to avoid this behavour. – Boris Pawlowski Apr 06 '15 at 12:52
  • Answered here - https://stackoverflow.com/a/46563020/7758211 – Rasool Mohamed Oct 04 '17 at 11:00
  • my case - receiver create multi instance so make sure it must be single and try to create instance in oncreate. – Agilanbu Dec 27 '21 at 12:23

3 Answers3

6

Put broadcast register line in onCreate and unregister it in onDestroy() method. The line which you have to move from onResume() to onCreate is:-

LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("my-event"));}
Pankaj
  • 7,908
  • 6
  • 42
  • 65
1

Possibly, you have two instances of the activity living at the same time. Make a breakpoint on the message receiver and check the address of the instance of your activity class and see if they are different each time the onReceive is called.

There are a few reasons why you could have two instances living at the same time, but one of the most common is leaking context within the activity. More on this topic.

Jose L Ugia
  • 5,960
  • 3
  • 23
  • 26
0

I fixed same problem by unregister BroadcastReceiver in onPause method

LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver,new IntentFilter("my-event")));

Register it in OnResume Method

LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("my-event"));}
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130
Manpreet Patil
  • 709
  • 1
  • 6
  • 13