I would like to pass a string to an Activity easily. Something like callback would be needed because when the string has to be passed then the Activity has to do something.
public class MyHostApduService extends HostApduService {
@Override
public byte[] processCommandApdu(byte[] apdu, Bundle extras) {
if (selectAidApdu(apdu)) {
Log.i("HCEDEMO", "Application selected");
return getWelcomeMessage();
}
else {
if (exchangeDataApdu(apdu)) {
Log.i("HCEDEMO", "Data exchanged");
// int _len_ = apdu[4];
String _data_ = new String(apdu).substring(5, apdu.length - 1);
// TODO: Send _data_ to an activity...
return new byte[] { (byte)0x90, (byte)0x00 };
}
else {
Log.i("HCEDEMO", "Received: " + new String(apdu));
return getNextMessage();
}
}
}
}
Your question is very broad and an answer depends on some design aspects of your application that you did not reveal to us:
You want to start the activity and pass some data to it
You can start the activity with an intent and pass the parameter as intent extra (also see this answer):
You want to pass the data to a running instance of your activity
In that case, you could, for instance, register a
BroadcastReceiver
from your activity:In your service, you could then send an intent to that broadcast receiver (note that you might want to restrict the broadcast with a receiver permission in order to prevent your data from leaking to unauthorized receivers):
You want to pass data to an activity and receive a response back into your service
Similar as above, you could use the broadcast receiver in your service to get notified from your activity. Note that you cannot bind to your HCE service, so you cannot directly invoke methods on it from your activity.
You have to use Intent mechanism.
If activity have to be started, in your service add:
and in activity: