What is the best practice for transfer of some (not huge, but often) amount of data from several services to activity?
I'm using BroadcastReceiver in my MainActivity this way:
private class Receiver extends BroadcastReceiver{
public Receiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
// Receiving and showing received symbols
if (intent.getAction() == getString(R.string.key_recognized)) {
char key = intent.getCharExtra("key" , '0');
MainActivity.this.addText(key);
}
// Received transfer ending flag
if (intent.getAction() == getString(R.string.transfer_ended)) {
mServiceWorking = false;
MainActivity.this.mToneButton.setText(getText(R.string.send_text));
}
// Recieving an array (~2Kb) and drawing it on correspondig ImageView
if (intent.getAction() == getString(R.string.spectrum_ready)) {
Spectrum spectrum = (Spectrum) intent.getSerializableExtra("spectrum");
if (spectrum != null) {
drawSpectrum(spectrum);
}
}
}
}
Broadcasts are sended from services somehow like this:
Intent taskIntent = new Intent();
taskIntent.setAction(getString(R.string.key_recognized));
taskIntent.putExtra("key", key);
this.sendBroadcast(taskIntent);
Is this normal code or my hands should be cut off in some way?)