Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Local Broadcast Manager saves duplicated data

I want to get location each minute, I have location monitoring service for this.

I call LocalBroadcastManager in MainActivity. For first time, It works normally, save my data to DB only once, but when I start MainActivity from other activity, it starts to increase number of saves, two times, three times...

I want to prevent to this problem

LocalBroadcastManager.getInstance(this).registerReceiver(
            new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String latitude = intent.getStringExtra(LocationMonitoringService.EXTRA_LATITUDE);
                    String longitude = intent.getStringExtra(LocationMonitoringService.EXTRA_LONGITUDE);

                    if (latitude != null && longitude != null) {

                        databaseHelper.addLocation(address,longitude,latitude,timeStr,dateStr);
                    }
                }
            }, new IntentFilter(LocationMonitoringService.ACTION_LOCATION_BROADCAST)
    );

I can show full code (This is short version).

Comments