Home > Blockchain >  Keep Android Qt apps running in background
Keep Android Qt apps running in background

Time:11-11

I've created a doorbell system (server, client) for my home which works via MQTT publish/subscriptions to know when someone rang the doorbell. It works quite well, however in my client, the MQTT connection keeps closing, even after setting _client->setAutoKeepAlive(true).

Moreover, I want to know if anyone can give me a hint on how to set the app to keep running in the background. What I found out so far is that I can set the persistence attribute in my AndroidManifest.xml, but is that all I can do to have my application run in background all the time, even if it gets closed accidentally?

My questions:

  1. How can I prevent QMqttClient from automatically disconnecting - or: how can I automatically reconnect if the connection gets lost?
  2. How do I prevent Qt Android apps from being killed?

CodePudding user response:

You can use the service to avoid program destruction as much as possible.

A Service is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.

The Android system stops a service only when memory is low and it must recover system resources for the activity that has user focus. If the service is bound to an activity that has user focus, it's less likely to be killed; if the service is declared to run in the foreground, it's rarely killed. If the service is started and is long-running, the system lowers its position in the list of background tasks over time, and the service becomes highly susceptible to killing—if your service is started, you must design it to gracefully handle restarts by the system.

https://developer.android.com/guide/components/services

  • Related