泛美卫生组织MQTT的Andr​​oid业务文档 [英] Paho MQTT Android Service Issue

查看:161
本文介绍了泛美卫生组织MQTT的Andr​​oid业务文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是我开发一个应用程序中实现泛美卫生组织MQTT Android的服务。测试由泛美卫生组织提供的示例应用程序后,我发现有一些我想改变一些东西。

https://eclipse.org/paho/clients/android/

在应用程序服务似乎关闭一旦应用程序完全关闭。我想保持业务运行状态。我也正在寻找一种方式来打开应用程序,一旦接收到新信息的特定活动,即使该应用程序的情况下更多的消息进来关闭。

下面是当邮件到达时调用的回调之一,我试图实现一个简单的startActivity打开特定的活动,但它不工作,如果应用程序是关闭/不再运行

如果任何人有泛美卫生组织MQTT Android的服务工作,也可把该服务时,在应用程序关闭停止具体方式,以及我怎样才能重新打开应用程序时,一个消息到达?

  / **
   * @see org.eclipse.paho.client.mqttv3.MqttCallback#messageArrived(java.lang.String中,
   * org.eclipse.paho.client.mqttv3.MqttMessage)
   * /
  @覆盖
  公共无效messageArrived(字符串话题,MqttMessage消息)抛出异常{

    //获取与此对象Connection对象关联
    连接C = Connections.getInstance(上下文).getConnection(clientHandle);

    //创建参数格式报文到达notifcation字符串
    字串[] args =新的String [2];
    ARGS [0] =新的String(message.getPayload());
    的args [1] =主题+;服务质量:+ message.getQos()+;保留:+ message.isRetained();

    //得到的strings.xml和格式字符串
    字符串messageString = context.getString(R.string.messageRecieved,(对象[])参数);

    //创建意图开始活动
    意向意图=新的意图();
    intent.setClassName(上下文中,org.eclipse.paho.android.service.sample.ConnectionDetails);
    intent.putExtra(处理,clientHandle);

    //格式字符串的args
    [对象] notifyArgs =新的String [3];
    notifyArgs [0] = c.getId();
    notifyArgs [1] =新的String(message.getPayload());
    notifyArgs [2] =话题;

    //通知用户
    Notify.notifcation(背景下,context.getString(R.string.notification,notifyArgs),意图,
        R.string.notifyTitle);

    //更新客户端的历史
    c.addAction(messageString);

    Log.e(消息到达,消息到达回调);

    //用于打开应用程序,如果它是当前不活动
    意图I =新的意图(背景下,ConnectionDetails.class);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra(处理,clientHandle);
    context.startActivity(ⅰ);


  }
 

解决方案

如果您使用任务管理器,我不认为这是可能的,因为完全关闭应用程序也将停止它包含的任何服务关闭应用程序。尽管该服务已启动粘性它不会重新启动我的设备上。如果您对近期的任务刷它拿走关闭应用程序的服务将保持运行。在这里看到更多的信息:<一href="http://stackoverflow.com/questions/11364342/killing-android-application-from-task-manager-kills-services-started-by-app">Killing从任务管理器Android应用程序启动杀敌通过应用服务

不过,我觉得另一个问题是,即使服务仍在运行的应用程序包含了服务被调用的回调对象。如果应用程序不再运行,回调不再存在,因此不会被调用。

下面是我如何实现这样的高级视图。这已在生产中已经运行了几个月,但不幸的是我没有自己的code,不能将它张贴。

  • 在我创建承载singleton对象的 MQTTService / mqttAndroidClient 。这暴露了公共的方法来连接/断开,并包含用于接收邮件的 MqttCallback 对象。它也处理丢失,需要重试机制的连接。这是最棘手的部分,但我不能将它张贴在这里。
  • 在我创建了一个应用程序的对象,我连在的onCreate()和关闭连接 onTerminate()
  • 在我注册了一个的BroadcastReceiver 是可以获得 BOOT_COMPLETED 操作驻留在应用程序的对象,它有一个空的实现,但它旋转起来的应用程序,这样的MQTT服务在启动连接。

这消除了任何给定的活动运行接收消息的需要。它似乎也对弹性关闭应用程序,如果你强行关闭在应用程序中设置的例外是。这使得自,但因为用户明确选择将其关闭。

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);


  }

解决方案

If you close your app using the task manager I don't think this is possible since "fully closing" the app will also stop any services it contains. Even though the service is started "sticky" it does not re-launch on my device. If you close the app by swiping it away on the recent tasks the service does remain running. See here for more info: Killing android application from task manager kills services started by app

However, I think the other problem is even if the service is still running the application contains the callback objects that get invoked by the service. If the application is no longer running the callback no longer exists and thus never gets called.

Here is a high level view of how I implemented this. This has been running in production for a few months but unfortunately I don't own the code and can't post it.

  • I created a singleton object that hosts the MQTTService/mqttAndroidClient. This exposes public methods to connect/disconnect, and contains the MqttCallback object used to receive the messages. It also handles the connection lost and retry mechanisms needed. This is the trickiest part but I can't post it here.
  • I created an Application object, I connect in onCreate() and close the connection in onTerminate()
  • I registered a BroadcastReceiver that gets the BOOT_COMPLETED action that resides in the Application object, it has an empty implementation but it spins up the application so the mqtt service connects on boot.

This eliminates the need for any given activity to be running to receive a message. It also seems resilient against closing the application, the exception being if you 'force close' it in the application settings. That makes since though since the user explicitly chose to close it.

这篇关于泛美卫生组织MQTT的Andr​​oid业务文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆