My android app has to communicate with a server. An activity thus creates an intent and starts an IntentService derivate.
Following some hints, like this one, I extended ResultReceiver to handle the answers: My activity implements the contained Receiver interface (thus implements onReceiveResult).
public class MyResultDistributor extends ResultReceiver {
// Class instances serves one purpose: To remember who gets the answer.
private Receiver mReceiver;
// Constructor: Apparently, the "handler" determines in which thread the result handling is done.
public MytResultDistributor(Handler handler) {
super(handler);
}
// This is what the receiver of request answers has to implement.
public interface Receiver {
public void onReceiveResult(int resultCode, Bundle resultData);
}
// Allows to clarify who is the receiver of request results.
public void setReceiver(Receiver receiver) {
if (receiver != null) {
Log.d(this.getClass().toString(), "Setting receiver.");
} else {
Log.d(this.getClass().toString(), "Unsetting receiver.");
}
this.mReceiver = receiver;
if (this.mReceiver != null) {
Log.d(this.getClass().toString(), "Set receiver.");
} else {
Log.d(this.getClass().toString(), "Unset receiver.");
}
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// Anybody interested in the results? Well, then feel free to take them.
Log.d(this.getClass().toString(), "Got result.");
if (this.mReceiver != null) {
Log.d(this.getClass().toString(), "Distributing result.");
this.mReceiver.onReceiveResult(resultCode, resultData);
} else {
Log.d(this.getClass().toString(), "No receiver there.");
}
}
The activity creates an instance of this, and sets itself as Receiver (excerpt):
public class MainActivity extends Activity implements MyResultDistributor.Receiver {
public MyResultDistributor mDistributor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(this.getClass().toString(), "onCreate");
setContentView(R.layout.activity_main);
mDistributor = new MyResultDistributor(new Handler());
mDistributor.setReceiver(this);
(...)
}
@Override
protected void onPause() {
super.onPause();
Log.v(this.getClass().toString(), "onPause");
mDistributor.setReceiver(null);
}
@Override
protected void onResume() {
super.onResume();
Log.v(this.getClass().toString(), "onResume");
mDistributor.setReceiver(this);
}
(...)
Now, when I run the activity, start my service, and it returns the result, it seems that mReceiver
is null. But I cannot see where it gets set to null in the log !??
Log excerpt:
onCreate
Setting receiver.
Set receiver.
onResume
Setting receiver.
Set receiver.
Request Service was created.
Request Service has got an Intent.
Server answer: auth
Authenticated!
Got result.
No receiver there.
where the last 5 messages feature a different TID.
Why does the receiver appear set in one method and unset in the other one? Do I have a thread problem? Maybe done something wrong by using new Handler()
?
Thank you for any hints!