I have a service running with MQTT.
Now when the phone goes in sleep mode (screen black), the mqtt disconnects.
When I wake up my phone the mqtt reconnects.
Can somebody explain this behavior? And how can I prevent this from happening?
You need to make the service a foreground service and use PhoneWakeState to acquire a partial lock. Note that on some devices it will still disconnect after a while.