I have a sticky unbound service which I start through a broadcast receiver on wifi i.e. my service starts when wifi turns on. My service has created a thread to perform some socket operation. Whenever socket makes a connection to remote device, I want to show in the text box, phone connected and when it's not it should show disconnected. I have tried following things without any success.
- I looked for bound services. But the examples I saw, showed a way to call the methods of my service class from MainActivity class. Not the other way round. Whenever connection state changes in Service's thread class I want to call a MainActivity's method to set the text of text box.
- I tried using callbacks through interface, where MainActivity implements interface. For that I need to pass a MainActivity's object to the thread class's constructor. But the thread which service creates takes no parameter. It's like making two object of thread java class. One by the service which doesn't take any parameter and other by MainActivity class which takes it's instance as parameter.
- I tried sending MainActivity instance to Service through Parceable. For that MainActivity has to implement Parceable, but when implementing a CREATER it throws error.
- I also followed this answer but can't understand what it's trying to do.
Either I am taking a wrong approach. Or I am missing something about above points. I want my text box content to change, whenever socket is connected or disconnected which is running in a thread created by an unbound service, called by a broadcast receiver.