Paho MQTT Android Service Issue
android, mqtt, paho, service
Solution
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:
- Service maintains a single instance as long as it is alive.
- If service is killed, Android restarts it (because START_STICKY)
- Service can be started when device boots.
- Service runs in the background and is always connected to receive notifications.
- 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.
Problem
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 few things that I would like to change. https://eclipse.org/paho/clients/android/ The applications service appears to shut off once the application is fully closed. I would like to keep the service running even after the application closes in the event more messages come in. I also am looking for a way to open the application to a specific activity once a new message is received. Here is one of the callbacks that is called when a message arrives, I have tried to implement a simple startActivity to open a specific activity but it does not work if the app is closed/no longer running. If anyone has worked with the PAHO MQTT Android Service, Is there a specific way to keep the service from stopping when the application is closed, and how can I re-open the application when a message arrives? ``` /** * @see org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.String, * org.eclipse.paho.client.mqttv3.MqttMessage) */ @Override public void messageArrived(String topic, MqttMessage message) throws Exception { // Get connection object associated with this object Connection c = Connections.getInstance(context).getConnection(clientHandle); // create arguments to format message arrived notifcation string String[] args = new String[2]; args[0] = new String(message.getPayload()); args[1] = topic + ";qos:" + message.getQos() + ";retained:" + message.isRetained(); // get the string from strings.xml and format String messageString = context.getString(R.string.messageRecieved, (Object[]) args); // create intent to start activity Intent intent = new Intent(); intent.setClassName(context, "org.eclipse.paho.android.service.sample.ConnectionDetails"); intent.putExtra("handle", clientHandle); // format string args Object[] notifyArgs = new String[3]; notifyArgs[0] = c.getId(); notifyArgs[1] = new String(message.getPayload()); notifyArgs[2] = topic; // notify the user Notify.notifcation(context, context.getString(R.string.notification, notifyArgs), intent, R.string.notifyTitle); // update client history c.addAction(messageString); Log.e("Message Arrived", "MESSAGE ARRIVED CALLBACK"); // used to open the application if it is currently not active Intent i = new Intent(context, ConnectionDetails.class); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.putExtra("handle", clientHandle); context.startActivity(i); } ```
Related problems
- How to have Android Service communicate with Activity
- Android service crashes after app is swiped out of the recent apps list
- Killing android application from task manager kills services started by app
- Implement Eclipse MQTT Android Client using single connection instance
- Paho MQTT Android service wake up activity