简体   繁体   English

如何在应用程序中保持XMPP连接活动?

[英]How to keep XMPP connection alive in the App?

I am currently working on an application that uses asmack lib to connect to a XMPP Server. 我目前正在使用asmack lib连接到XMPP服务器的应用程序。 This application basically includes Sending/receiving messages, changing statuses, etc. 该应用程序主要包括发送/接收消息,更改状态等。

At the moment, the XMPP connection lives inside the application and isn't in some sort of background service. 目前,XMPP连接存在于应用程序内部,而不是某种后台服务。 So now I'm wondering, is it better to keep the connection alive using a service or just keeping the connection alive when my application is actually running. 所以现在我想知道,使用服务保持连接是否更好或者只是在我的应用程序实际运行时保持连接处于活动状态。

This is taking in consideration that I want to stay connected to the XMPP Server the entire time when my application is running background and when user come back to any activity having XMPP connection. 这是考虑到我希望在我的应用程序运行后台以及用户返回任何具有XMPP连接的活动时始终保持与XMPP服务器的连接。 i did like this, if it comes to main activity ( means where i am connecting with credentials ) re-connecting the XMPP connection with same credentials. 我确实喜欢这个,如果它涉及主要活动(意味着我连接凭证)重新连接XMPP连接与相同的凭据。 but i am facing problem that when i stay for some time in contacts view, the connection is getting closed after some time if that activity resumes back getting foreclose at connection ( ie null pointer exception ).Here it is not possible to re-connect the connection. 但我面临的问题是,当我在联系人视图中停留一段时间后,连接在一段时间后会被关闭,如果该活动恢复到连接时取消预告(即空指针异常)。这里是不可能重新连接连接。

So in a way I'm asking if it's better to (re)connect/log-in as soon as my Activity is brought to the foreground/started or is it better to connect once inside a service and just keep this connection alive? 所以在某种程度上,我问一下,当我的Activity被带到前台/开始时,(重新)连接/登录是否更好,或者最好在服务内部连接一次并保持这个连接活着?

If service creation is better way, How to create a from fragments and how to create XMPP connection and i have to do log-in and log-out by using buttons .how to maintain these options in service. 如果服务创建是更好的方法,如何创建片段以及如何创建XMPP连接,我必须使用按钮进行登录和注销。如何在服务中维护这些选项。

Thanks in advance, 提前致谢,

If you want to be connected to the XMPP server all the time, Service is the way to go. 如果您希望始终连接到XMPP服务器,那么Service就是您的选择。

So once the user log in, you can start the communication service and keep it running and when the user log out stop the service 因此,一旦用户登录,您就可以启动通信service并使其保持运行,并在用户注销时停止service

You can show notifications from your service that will open the activity when it is clicked. 您可以显示来自您的service通知,该通知会在单击时打开该活动。

If you have simple communication (eg pass few commands) between your service and your activity you can do this using LocalBroadcastManager , but if your communication is more complex (eg activity listen to event in the service) consider creating a service binder that is used from the activity 如果您在服务和活动之间进行简单的通信(例如传递少量命令),则可以使用LocalBroadcastManager执行此操作,但如果您的通信更复杂(例如,活动侦听服务中的事件),请考虑创建一个使用的service binder器。 activity

This an example skeleton of a service that supports bind 这是支持bind的服务的示例框架

public class MyService extends Service {
    private final IBinder binder = new ServiceBinder();

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
      onHandleIntent(intent);
      return START_STICKY;
   }

   protected void onHandleIntent(Intent intent) {
       // handle intents passed using startService()
   }

   @Override
   public IBinder onBind(Intent intent) {
    return binder;
   }

   public class ServiceBinder extends Binder {
        MyService getService() {
            return MyService.this;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM