Similar callbacks (observer pattern) that you show in your example will not work between service and activity. Use an observer application if you created an instance of class B from class A and want to send callbacks from B to A.
As for services and activities, everything is completely different. AFAICT, if you want to cancel your Activity from Service , the best way to achieve this is to use ResultReceiver . There are a lot of interesting things ResultReceiver :
- Its constructor gets a
Handler (which you must create inside the action), which allows you to change the user interface from the service. - It implements
Parcelable , so you can put the link of your ResultReceiver in the optional Intent functions that you used to run the service. - Its
onReceive method has an integer result code that allows you to generate different callbacks (it looks like there were a lot of methods in your callback interface). In addition, he receives a Bundle , which you can use to place all the result data.
On the other hand, if you want to make a callback (not sure if this is the right term in this case), from your Activity to your Service , I think you will have to send a broadcast message or something like that.
source share