Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast received twice

I am using a local broadcast to let my service know that the AsyncTask has finished its work but I have a small issue : the broadcast is only sent once (it is created by a function that is only called when the app is launched) but I receive it twice.

simplified code :

@Override
protected void onPostExecute(HttpResponse result) {
    LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getBaseContext());
    localBroadcastManager.sendBroadcast(new Intent(getString(R.string.bc_CONNECTED)));
}

in the service:

private BroadcastReceiver connectedBroadcastReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(getString(R.string.app_tag), "broadcast received !!");
    }
};

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    LocalBroadcastManager.getInstance(this).registerReceiver(connectedBroadcastReceiver, new IntentFilter(getString(R.string.bc_CONNECTED)));
    return START_STICKY;
}

Has anyone encountered such a weird behavior yet?

like image 466
Teovald Avatar asked Sep 16 '12 16:09

Teovald


1 Answers

I had the same issue, the problem was that I was registrering the receiver in the onCreate method and in the onResume method. Removing from the onCreate the problem was solved.

like image 82
Oriol Avatar answered Sep 27 '22 21:09

Oriol