Implement Eclipse MQTT Android Client using single connection instance

后端 未结 2 996
予麋鹿
予麋鹿 2020-12-17 04:56

I am using Eclipse Paho android mqtt service in my app. I am able to subscribe and publish the messages to mqtt broker. I have couple of Activities in the app, when any acti

2条回答
  •  囚心锁ツ
    2020-12-17 05:50

    A 'better' way would be to create a Service which connects/reconnects to the MQTT Broker.

    I created my own service called MqttConnectionManagerService which maintains and manages connection to the broker.

    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() method (and not onCreate()). In this method, we simply check if this client is connected to the broker and connect/reconnect if required.

    Sample code:

    MqttConnectionManagerService

    public class MqttConnectionManagerService extends Service {
    
        private MqttAndroidClient client;
        private MqttConnectOptions options;
    
        @Override
        public void onCreate() {
            super.onCreate();
            options = createMqttConnectOptions();
            client = createMqttAndroidClient();
        }
    
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            this.connect(client, options);
            return START_STICKY;
        }
    
        private MqttConnectOptions createMqttConnectOptions() {
            //create and return options
        }
    
        private MqttAndroidClient createMqttAndroidClient() {
            //create and return client
        }
    
        public void connect(final MqttAndroidClient client, MqttConnectOptions options) {
    
            try {
                if (!client.isConnected()) {
                    IMqttToken token = client.connect(options);
                    //on successful connection, publish or subscribe as usual
                    token.setActionCallback(new IMqttActionListener() {..});
                    client.setCallback(new MqttCallback() {..});
                }
            } catch (MqttException e) {
                //handle e
            }
        }
    
    }
    

    AndroidManifest.xml

    
    
    
        
        
    
        
    
            
    
            
            
                
                    
                
            
    
            
            
            
        
    
    
    

    MqttServiceStartReceiver

    public class MqttServiceStartReceiver extends BroadcastReceiver {    
        @Override
        public void onReceive(Context context, Intent intent) {
            context.startService(new Intent(context, MqttConnectionManagerService.class));
        }
    }
    

    In your Activity's onResume()

    startService(new Intent(this, MqttConnectionManagerService.class));
    

提交回复
热议问题