I am creating an instant messaging system using Paho Android MQTT Client. Its implementation works as expected, however I am facing these errors.
I call the Connection Class
( This also calls for the creation of the connection to the broker) in the onCreate
of the MainActivity Class
.
Now the problem is that, assuming I am on the MainActivity Class
and I press back to move from the MainActivity Class
to another activity, and I later come back to the MainActivity Class
, another broker connection will be created. This will mean that anytime a single message is published, the client will receive the message twice.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_intera);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
dbHelper = DatabaseManager.getInstance(context);
//mRecyclerView = (RecyclerView) findViewById(R.id.history_recycler_view);
connections = new Connections();
connections.createConnectionForPublishing(context);
}
@Override
public void onDestroy() {
super.onDestroy();
System.out.println("LOG: Service destroyed");
}
}
Connection.java
public class Connection {
public void createConnectionForPublishing (final Context context) {
//Instantiate the mqtt android client class
mqttAndroidClient = new MqttAndroidClient (context.getApplicationContext (), serverUri, clientId);
mqttAndroidClient.setCallback (new MqttCallbackExtended () {
@Override
public void connectComplete(boolean reconnect, String serverURI) {
if (reconnect) {
System.out.println ("Reconnected to : " + serverURI);
} else {
System.out.println ("Connected to: " + serverURI);
}
}
@Override
public void connectionLost (Throwable cause) {
System.out.println ("The Connection was lost.");
}
@Override
public void messageArrived (String topic, final MqttMessage message) throws Exception {
System.out.println ("Message received and Arrived");
}
@Override
public void deliveryComplete (IMqttDeliveryToken token) {
System.out.println("Message Delivered");
}
});
final MqttConnectOptions mqttConnectOptions = new MqttConnectOptions ();
mqttConnectOptions.setMqttVersion (MqttConnectOptions.MQTT_VERSION_3_1_1);
mqttConnectOptions.setAutomaticReconnect (true);
mqttConnectOptions.setCleanSession (false);
try {
mqttAndroidClient.connect (mqttConnectOptions, null, new IMqttActionListener () {
@Override
public void onSuccess (IMqttToken asyncActionToken) {
System.out.println ("BROKER CONNECTED");
DisconnectedBufferOptions disconnectedBufferOptions = new DisconnectedBufferOptions ();
disconnectedBufferOptions.setBufferEnabled (true);
disconnectedBufferOptions.setBufferSize (100);
disconnectedBufferOptions.setPersistBuffer (false);
disconnectedBufferOptions.setDeleteOldestMessages (false);
//mqttAndroidClient.setBufferOpts (disconnectedBufferOptions);
}
@Override
public void onFailure (IMqttToken asyncActionToken, Throwable exception) {
System.out.println ("Failed to connect to: " + serverUri);
}
});
} catch (MqttException ex) {
ex.printStackTrace ();
}
}
// ...
}
I am new to MQTT, I would be glad if somebody can help. Thanks in advance
Then if you don't want to establish the connection multiple times you'll have to make it as singleton to have only 1 instance of the connection class and one time connection established.
So your code will become:
MainActivity.java:
Connection.java
I'm not sure but I think maybe a null check can help that
I suggest that you set a flag using Android
onPause()
https://developer.android.com/reference/android/app/Activity.html#onPause()
And then instead of using
onCreate()
, useonResume()
to initiate your MQTT connection but checkonPause()
flag first.You may also need to close the connection in
onPause()
oronDestroy()
. Look at lifetime of an Android application/activity for more info.