【发布时间】:2015-04-17 02:10:10
【问题描述】:
我正在开发的应用程序中实现 Paho MQTT Android 服务。在测试了 Paho 提供的示例应用程序后,我发现有几处我想更改。
https://eclipse.org/paho/clients/android/
应用程序服务似乎在应用程序完全关闭后关闭。即使在应用程序关闭后,如果有更多消息进入,我也希望保持服务运行。我还在寻找一种方法,一旦收到新消息,就可以将应用程序打开到特定活动。
这是消息到达时调用的回调之一,我尝试实现一个简单的 startActivity 来打开特定的活动,但如果应用程序关闭/不再运行,它就不起作用>.
如果有人使用过 PAHO MQTT Android 服务,是否有特定的方法可以在应用程序关闭时防止服务停止,以及当消息到达时如何重新打开应用程序?强>
/**
* @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);
}
【问题讨论】:
-
你有没有办法解决这个问题?
-
不幸的是,我没有。我开始使用另一个示例。如果我找到一个解决方案,我会发布一个。
-
您现在解决问题了吗?或者至少你找到了其他一些可行的(令人满意的)例子?
-
我也有同样的问题。
-
我也有同样的问题