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?
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.
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