Paho MQTT Android Service Issue

后端 未结 4 1683
离开以前
离开以前 2020-12-30 11:56

I am implementing the Paho MQTT Android Service within an application I am developing. After testing the sample application provided by Paho, I have found that there are a f

4条回答
  •  独厮守ぢ
    2020-12-30 12:15

    I know this is a late answer to this question but I would like to share what I've done as it may help someone.

    I created my own Service to manage connection to the broker and always maintain a single connected instance per android device.

    Reiterating the features of the solution:

    Key features of this solution:

    1. Service maintains a single instance as long as it is alive.
    2. If service is killed, Android restarts it (because START_STICKY)
    3. Service can be started when device boots.
    4. Service runs in the background and is always connected to receive notifications.
    5. If the service is alive, calling startService(..) again would trigger its onStartCommand(). In this method, we simply check if this client is connected to the broker and connect/reconnect if required.

    Check out the fully detailed answer here.

提交回复
热议问题