Here ist the example of MQTT as a service.
You have to start and stop it with the checkbox, otherwise it will run 4 ever in background even if the activity is closed

It's roughly programmed just as a functioning skeletton you can tweek to your needs, have fun. (any likes welcome

)